JAVA实验报告河北工业大学Word文件下载.docx

上传人:b****6 文档编号:16362653 上传时间:2022-11-23 格式:DOCX 页数:16 大小:453.11KB
下载 相关 举报
JAVA实验报告河北工业大学Word文件下载.docx_第1页
第1页 / 共16页
JAVA实验报告河北工业大学Word文件下载.docx_第2页
第2页 / 共16页
JAVA实验报告河北工业大学Word文件下载.docx_第3页
第3页 / 共16页
JAVA实验报告河北工业大学Word文件下载.docx_第4页
第4页 / 共16页
JAVA实验报告河北工业大学Word文件下载.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

JAVA实验报告河北工业大学Word文件下载.docx

《JAVA实验报告河北工业大学Word文件下载.docx》由会员分享,可在线阅读,更多相关《JAVA实验报告河北工业大学Word文件下载.docx(16页珍藏版)》请在冰豆网上搜索。

JAVA实验报告河北工业大学Word文件下载.docx

importjava.awt.*;

importjava.applet.Applet;

publicclassHelloAppletextendsApplet

publicvoidpaint(Graphicsg)

g.setColor(Color.red);

g.drawString("

Hello!

20,20);

}

 

实验二Java语法基础

一、声明不同数据类型变量

1、编写声明不同数据类型变量的程序。

publicclassSimpleTypes

{publicstaticvoidmain(Stringargs[])

{byteb=0x55;

shorts=0x55ff;

inti=1000000;

longl=0xfffL;

charc='

c'

;

floatf=0.23F;

doubled=0.7E-3;

booleanbool=true;

b="

+b);

System.out.println("

s="

+s);

i="

+i);

l="

+l);

c="

+c);

f="

+f);

d="

+d);

bool="

+bool);

2、Integer类在某对象中打包了原始类型为int的值。

Integer类型对象包含int型的单个域。

此外,此类提供了许多方法,可以将int型转换为string型,也可以将Sring型转换为int型,还包含处理int类型时的其他有用常量和方法。

publicclassIntegerDemo

{publicstaticvoidmain(Stringargs[])

{Integer[]array={newInteger(20),newInteger(40),newInteger("

110"

)};

for(inti=0;

i<

array.length;

i++)

{System.out.println(Integer.toBinaryString(array[i].intValue())+"

\t"

System.out.println(Integer.toHexString(array[i].intValue())+"

System.out.println(Integer.toOctalString(array[i].intValue())+"

\n"

二、分支程序结构

1、使用if...else语句

publicclassTestIf

{

publicstaticvoidmain(Stringargs[])

booleanleap;

intyear=2005;

if((year%4==0&

&

year%100!

=0)||(year%400==0))

System.out.println(year+"

年是闰年"

else

System.out.println(year+"

年不是闰年"

year=2008;

//方法2

if(year%4!

=0)

leap=false;

elseif(year%100!

leap=true;

elseif(year%400!

if(leap==true)

year=2050;

//方法3

if(year%4==0){

if(year%100==0){

if(year%400==0)

}

}

2、switch多分支语句

用swith语句实现从键盘读如1,2,3时,屏幕提示不同信息。

classSwitch

{publicstaticvoidmain(Stringargs[])throwsjava.io.IOException

{chara;

Enteranumberfrom1--3"

a=(char)System.in.read();

switch(a)

{

case'

1'

:

winaCar!

break;

2'

pickedthegoat"

3'

gettokeepyour100"

default:

entry"

3、用分支语句编程,输入一个学生成绩,给出相应等级:

publicclassprog

try{

bytebuf[]=newbyte[50];

System.in.read(buf);

intn=Integer.parseInt(newString(buf).trim());

if(n>

=0&

n<

=100)

=90)

优"

elseif(n>

=80)

良"

=70)

中"

=60)

及格"

else

不及格"

}catch(Exceptione){

三、循环及跳转程序结构

1、分析程序,掌握for控制语句,写出执行结果。

publicclassTesFor

{publicstaticvoidmain(Stringargs[])

throwsIOException

intfahr;

intcels;

CelsiusFahrenheit"

for(cels=0;

cels<

=20;

cels+=5)

{fahr=cels*9/5+32;

System.out.println(cels+"

"

+fahr);

chara;

outer:

5;

{for(intj=0;

j<

j++)

{a=(char)System.in.read();

if(a=='

b'

)break;

)continue;

2、用三种循环语句来分别实现输出10~50之间的3的倍数。

publicclassthree

{publicstaticvoidmain(Stringargs[])

{inti=0;

for(i=10;

=50;

if(i%3==0)

System.out.println(i);

publicclassthreee{

privateinti=10;

publicvoidfunc(){

while(i<

50){

if(i%3==0){

System.out.print(i+"

);

i++;

publicstaticvoidmain(Stringargs[]){

threeet=newthreee();

t.func();

classthre

privateinti=10;

publicvoidfunc(){

do{

if(i%3==0)

System.out.print(i+"

}while(i<

50);

publicstaticvoidmain(Stringargs[])

{thret=newthre();

t.func();

3、打印以下图案(每行打5个星号,每个星号之间空两个空格)。

classstar

inti,j,k;

for(i=0;

if(i>

0)

for(k=0;

k<

i;

k++)

System.out.print("

for(j=0;

{System.out.print("

*"

if(j==4)

}}}

2、采用for循环求1至1000之内的所有“完全数”。

所谓“完全数”是指一个数,恰好等于它的因子之和。

例如,6是一个完全数,因为6的因子为1、2、3,而6=1+2+3。

publicclassnum{

publicstaticvoidmain(Stringargs[]){

intcount=1;

numpn=newnum();

for(inti=1;

i<

1000;

i++){

if(pn.isPerfect(i)){

count++;

if(count%3==3)

System.out.println();

booleanisPerfect(intx){

inty=0;

x;

if(x%i==0)

y=y+i;

if(x==y)

returntrue;

returnfalse;

3、一个整数的各位数字之和能被9整除,则该数也能被9整除。

编程验证给定的整数能否被9整除。

importjava.util.*;

classnine{

Scannerin=newScanner(System.in);

inti=in.nextInt();

if(i%9==0)

System.out.print("

能"

不能"

}}

4、已知XYZ+YZZ=532,其中X、Y和Z为数字,编程求出X,Y和Z的值。

classxyz

publicstaticvoidmain(Stringargs[]){

intx=0,y=0,z=0;

for(x=0;

x<

=9;

x++){

for(y=0;

y<

y++){for(z=0;

z<

z++){

if((z+z==2)&

(y+z==3)&

(x+y==5))

{System.out.println("

x的值:

+x);

y的值:

+y);

z的值:

+z);

}}}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 设计艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1