实验2 Java语言基础.docx

上传人:b****5 文档编号:10166778 上传时间:2023-02-09 格式:DOCX 页数:24 大小:69.14KB
下载 相关 举报
实验2 Java语言基础.docx_第1页
第1页 / 共24页
实验2 Java语言基础.docx_第2页
第2页 / 共24页
实验2 Java语言基础.docx_第3页
第3页 / 共24页
实验2 Java语言基础.docx_第4页
第4页 / 共24页
实验2 Java语言基础.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

实验2 Java语言基础.docx

《实验2 Java语言基础.docx》由会员分享,可在线阅读,更多相关《实验2 Java语言基础.docx(24页珍藏版)》请在冰豆网上搜索。

实验2 Java语言基础.docx

实验2Java语言基础

山西大学计算机与信息技术学院

实验报告

姓名

糜鹏程

学号

2010242451

专业班级

软件工程四班

课程名称

Java实验

实验日期

成绩

指导教师

批改日期

实验名称

实验2Java语言基础

一、实验目的

1、了解Java的数据类型;

2、掌握各种变量的声明方式;

3、理解运算符的优先级;

4、掌握Java基本数据类型、运算符与表达式、数组的使用方法;

5、理解Java程序语法结构,掌握顺序结构、选择结构和循环结构语法的程序设计方法;

6、通过以上内容,掌握Java语言的编程规则。

 

二、实验要求

1、编写一个声明Java不同数据类型变量的程序;

2、编写一个使用运算符、表达式、变量的程序;

3、编写表达式语句、复合语句的程序;

4、编写使用不同选择结构的程序;

5、编写使用不同循环结构结构的程序。

 

三、实验内容(按要求编程并贴图,并将图按顺序编号命名)

 

(一)声明不同数据类型的变量

1、编写声明不同数据类型变量的程序文件SimpleTypes.java,源代码如下:

publicclassSimpleTypes{

publicstaticvoidmain(String[]args){

byteb=0x55;

shorts=0x55ff;

inti=1000000;

longl=0xfffL;

charc='c';

floatf=0.23F;

doubled=0.7E-3;

booleanbool=true;

StringStr="这是字符串数据类型“;

System.out.println("字节型变量b="+b);

System.out.println("短整形变量s="+s);

System.out.println("整型变量i="+i);

System.out.println("长整型变量l="+l);

System.out.println("字符型变量c="+c);

System.out.println("浮点型变量f="+f);

System.out.println("双精度变量d="+d);

System.out.println("布尔型变量bool="+bool);

System.out.println("字符串类对象Str=”+str);

}

}

2、编译并运行该程序,结果如图1所示。

图1程序SimpleTypes.java的编译及运行结果贴图

(二)了解各种类型的取值范围和变量的使用范围

 

1、使用System.out.printf方法把所有基本数据类型的最大值最小值表示出来。

如int类型的取值范围System.out.printf("int\t数值范围:

%d~%d\n",Integer.MAX_VALUE,Integer.MIN_VALUE);

importjava.io.*;

publicclassLX2_5

{

publicstaticvoidmain(Stringargs[])throwsIOException{

System.out.printf("int\t数值范围:

%d~%d\n",Integer.MAX_VALUE,Integer.MIN_VALUE);

System.out.printf("float\t数值范围:

%f~%f\n",Float.MAX_VALUE,Float.MIN_VALUE);

System.out.printf("double\t数值范围:

%e~%e\n",Double.MAX_VALUE,Double.MIN_VALUE);

System.out.printf("byte\t数值范围:

%d~%d\n",Byte.MAX_VALUE,Byte.MIN_VALUE);

}

 

}

运行结果贴图

2、Scope.java文件,通过本程序了解变量的使用范围,源代码如下:

/Scope.java

//Java中变量的作用域

publicclassScope{

publicstaticvoidmain(String[]args){

intx=25;

System.out.println("x="+x);//只有x有效

{

inty=36;

System.out.println("x="+x);

System.out.println("y="+y);//x,y均有效

}

System.out.println("x="+x);

System.out.println("y="+y);

//只有x有效,y“outofscope”

}

}/

2、编译Scope.java

此时会出现错误提示如图2所示。

因为变量y在方法块中声明,在方法块之外它是不存在的,所以编译时会出错。

程序运行过程贴图

3、修改上面的程序。

4、成功运行该程序。

思考:

Scope.java程序说明了什么问题?

(三)使用关系运算符和逻辑运算符

1、建立使用关系运算符RealtionOp.java和逻辑运算符LogicOp.java的程序文件,源代码如下:

//RelationOp.java

//Java中关系运算符的使用

publicclassRelationOp{

publicstaticvoidmain(String[]args){

inta=9;

intb=6;

intc=6;

booleand=a>b;//ture

booleane=a

booleanf=b==c;//ture

booleang=b!

=c;//false

f=(b==c)||(a

g=(b==c)&&(a

System.out.println("d="+d);

System.out.println("e="+e);

System.out.println("f="+f);

System.out.println("g="+g);

}

}

运行结果贴图

//LogicOp.java

//Java中逻辑运算符的使用

publicstaticvoidmain(String[]args){

inta=9;

intb=6;

intc=6;

booleand,e,f,g;

d=!

(a>b);//false

e=(a>b)&&(a>c);//ture

booleanh=b>=c;//ture

booleani=b<=c;//ture

booleanj=a==b;//false

System.out.println("d="+d);

System.out.println("e="+e);

System.out.println("f="+f);

System.out.println("g="+g);

System.out.println("h="+h);

System.out.println("i="+i);

System.out.println("j="+j);

}

2、编译并运行该程序。

 

运行结果贴图

(四)使用表达式语句与复合语句

 

1.建立包含表达式语句程序,源代码如下。

classLX2_5{

publicstaticvoidmain(String[]args){

intk,i=3,j=4;

k=20*8/4+i+j*i;

System.out.println("表达式(20*8/4+i+j*i)="+k);

}

}

2.建立包含复合语句程序,源代码如下。

classLX2_6{

publicstaticvoidmain(Stringargs[]){

intk,i=3,j=4;

k=i+j;

System.out.println("在复合块外的输出k="+k);

{

floatf;

f=j+4.5F;

i++;

System.out.println("在复合块内的输出f="+f);

System.out.println("在复合块内的输出k="+k);

}

System.out.println("在复合块外的输出i="+i);

}

}

3.编译并运行上述两个源程序,结果如图

 

程序运行编译及运行过程贴图

4.将变量i在块内定义会怎样?

改变其他变量的位置看看会发生什么变化。

思考:

指出程序的复合结构以及变量的使用范围。

(五)使用选择语句

 

1.使用if...else语句

(1)程序功能:

使用if...else语句构造多分支,判断某一年是否为闰年。

闰年的条件是符合下面二者之一:

能被4整除,但不能被100整除;能被4整除,又能被100整除。

(2)编写源程序文件,代码如下

importjava.util.*;

publicclassTest1

{

//编写程序

publicstaticvoidtest(intyear)

{

if(year%400==0||(year%4==0&&year%100!

=0))

System.out.println("是闰年!

");

else

System.out.println("不是闰年!

");

}

publicstaticvoidmain(String[]args)

{

intyear;

Scannerinput=newScanner(System.in);

for(inti=0;i<4;i++)

{

year=input.nextInt();

test(year);

}

}

}

(3)编译运行程序,其结果如图所示。

程序运行编译及运行过程贴图

 

2.使用switch语句

(1)程序功能:

在不同温度时显示不同的解释说明。

温度c<10度,显示某某℃有点冷。

要多穿衣服

10≤c<25度,显示某某℃正合适。

出去玩吧。

25≤c<35度,显示某某℃℃有点热。

c≥35,显示某某℃太热了!

开空调。

上面的某某用变量c的值替换

(2)程序源代码如下。

classLX2_8{

publicstaticvoidmain(Stringargs[]){

intc=38;

 

switch(c<10?

1:

c<25?

2:

c<35?

3:

4){

case1:

System.out.println(""+c+"℃有点冷。

要多穿衣服。

");

case2:

System.out.println(""+c+"℃正合适。

出去玩吧。

");

case3:

System.out.println(""+c+"℃有点热。

");

default:

System.out.println(""+c+"℃太热了!

开空调。

");

}

}

}

(3)编译运行程序,其结果如图所示。

程序运行编译及运行过程贴图

(六)使用循环语句

 

1.for循环语句练习

(1)程序功能:

按5度的增量打印出一个从摄氏温度到华氏温度的转换表。

(2)程序源代码如下。

classLX2_9{

publicstaticvoidmain(Stringargs[]){

inth,c;

System.out.println("摄氏温度华氏温度");

 

for(c=0;c<=40;c+=5){

h=c*9/5+32;

System.out.println(""+c+""+h);

}

}

}

(3)编译并运行程序,其结果如图

程序运行编译及运行过程贴图

 

2.while循环语句练习

(1)程序功能:

运行程序后从键盘输入数字1/2/3后,可显示抽奖得到的奖品;如果输入其它数字或字符显示“没有奖品给你!

”。

(2)程序源代码如下。

importjava.io.*;

classLX2_10

{

publicstaticvoidmain(Stringargs[])throwsIOException{

charch;

System.out.println("按1/2/3数字键可得大奖!

");System.out.println("按空格键后回车可退出循环操作.");while((ch=(char)System.in.read())!

=''){System.in.skip

(2);//跳过回车键

switch(ch){

case'1':

System.out.println("恭喜你得大奖,一辆汽车!

");break;

case'2':

System.out.println("不错呀,你得到一台笔记本电脑!

");

break;

case'3':

System.out.println("没有白来,你得到一台冰箱!

");break;

default:

System.out.println("真不幸,你没有奖品!

下次再来吧。

");

}

}

}

}

(3)编译源程序。

(4)在命令提示符窗口运行程序,然后分别按1、2、3、r结果如图10所示。

程序运行编译及运行过程贴图

3.do…while循环语句练习

 

(1)程序功能:

求1+2+…+100之和,并将求和表达式与所求的和显示出来。

(2)程序源代码如下。

程序代码

importjava.io.*;

publicclassLX2_5

{

publicstaticvoidmain(Stringargs[])throwsIOException{

inti=1,s=0;

do{System.out.print(i+"+");

s+=i;

i++;}

while(i<=100);

System.out.println("="+s);

}}

(3)编译并运行程序,结果如图1所示。

程序运行编译及运行过程贴图

作业题:

 

课后题

2.11、

importjava.util.*;

publicclasstest{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Scannerscanner=newScanner(System.in);

System.out.print("Enteremployee.sname:

");

Stringk=scanner.nextLine();

System.out.println();

System.out.print("Enternumbeiofhoursworkedinaweek:

");

Floatl=scanner.nextFloat();

System.out.println();

System.out.print("Enterhourlypayrate:

");

Floato=scanner.nextFloat();

System.out.println();

System.out.print("Enterfederaltaxwithholdingrate:

");

Floatm=scanner.nextFloat();

System.out.println();

System.out.print("Enterstatetaxwithholdingrate:

");

Floatn=scanner.nextFloat();

System.out.println();

System.out.println("Employeename:

"+k);

System.out.println("HoursWorked:

"+l);

System.out.println("PayRate:

"+o);

System.out.println("GrossPay:

"+(l*o));

System.out.println("Deductions:

");

System.out.println("Federalwithholding(20.0%):

"+(l*o*m));

System.out.println("Federalwithholding(9.0%):

"+(l*o*n));

System.out.println("TotalDeduction:

"+((l*o*m)+(l*o*n)));

System.out.println("NetPay:

"+((l*o)-(l*o*m)-(l*o*n)));

}

}

方法二:

importjava.util.*;

importjavax.swing.JOptionPane;

publicclasstest{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Stringa=JOptionPane.showInputDialog("Enteremployee'sname:

");

Stringk=a;

a=JOptionPane.showInputDialog("Enternumbeiofhoursworkedinaweek:

");

Floatl=Float.parseFloat(a);

a=JOptionPane.showInputDialog("Enterhourlypayrate:

");

Floato=Float.parseFloat(a);

a=JOptionPane.showInputDialog("Enterfederaltaxwithholdingrate:

");

Floatm=Float.parseFloat(a);

a=JOptionPane.showInputDialog("Enterstatetaxwithholdingrate:

");

Floatn=Float.parseFloat(a);

System.out.println();

System.out.println("Employeename:

"+k);

System.out.println("HoursWorked:

"+l);

System.out.println("PayRate:

"+o);

System.out.println("GrossPay:

"+(l*o));

System.out.println("Deductions:

");

System.out.println("Federalwithholding(20.0%):

"+(l*o*m));

System.out.println("Federalwithholding(9.0%):

"+(l*o*n));

System.out.println("TotalDeduction:

"+((l*o*m)+(l*o*n)));

System.out.println("NetPay:

"+((l*o)-(l*o*m)-(l*o*n)));

}

}

 

2.18

publicclasstest{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

inta=1,b=2;

System.out.println("\ta"+"\tb"+"\tpow(a,b)");

for(inti=0;i<5;i++){System.out.println("\t"+a+"\t"+b+"\t"+(int)Math.pow(a,b));

a++;

b++;}

}

}

3.17

importjava.util.*;

publicclasstest{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

inta=(int)(Math.random()*4);

Scannerinput=newScanner(System.in);

System.out.println("scissor

(1),rock(0),paper

(2)");

intb=input.nextInt();

if((a==0&b==0)||(a==1&b==1)||(a==2&b==2)){System.out.println("Itisadraw");}

elseif((a==0&b==1)||(a==1&b==2)||(a==2&b==0)){System.out.println("Youwin");}

elseSystem.out.println("Youlose");

}

}

 

3.22

importjava.util.*;

publicclasstest{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

inta,b;

System.out.println("Enterapointwithtwocoordinates:

");

Scannerpoint=newScanner(System.in);

a=point.nextInt();

b=point.nextInt();

if(a*a+b*b>100)System.out.println("point("+a+b+")isnotinthecircle");

elseSyst

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

当前位置:首页 > 农林牧渔 > 林学

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

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