java实验1报告
- 格式:doc
- 大小:239.00 KB
- 文档页数:7
实验一:Java 基本语法
实验目的:
了解 Java 的数据类型,掌握各种变量的声明方式,理解运算符的优先级,掌握 Java 基本数据类型、运算符与表达式,掌握顺序结构、选择结构和循环结构语法的程序设计方法。
实验要求:
1、编写一个声明 Java 不同数据类型变量的程序。
2、编写使用不同选择结构的程序。
3、编写使用不同循环结构结构的程序。
实验内容:
1、声明不同数据类型变量
1)编写声明不同数据类型变量的程序,代码见实验指导书。
public class SimpleTypes{
public static void main(String args[]){
byte b=0x55;
short s=0x55ff;
int i=1000000;
long l=0xfffl;
char c='c';
float f=0.23f;
double d=0.7E-3;
boolean bool=true;
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);
}
}
运行结果:
2、使用选择结构
(一)使用 if...else 语句,
/**
* @(#)TestIf.java
*
*
* @author
* @version 1.00 2013/4/20
*/
class TestIf {
public static void main (String args[]){
boolean leap;
int year=2005;
if((year%4==0&&year%100!=0)||(year%400==0))// 方法1
System.out.println(year+"是闰年");
else
System.out.println(year+"不是闰年");
year=2008;// 方法2
if(year%4!=0)
leap=false;
else if(year%100!=0)
leap=true;
else if(year%400!=0)
leap=false;
else
leap=true;
if(leap==true)
System.out.println(year+"是闰年");
else
System.out.println(year+"不是闰年"); year=2050;// 方法3
if(year%4==0){
if(year%100==0)
leap=true;
else
leap=false;
}
else
leap=false;
if (leap==true)
System.out.println(year+"是闰年");
else
System.out.println(year+"不是闰年");
}
}
运行结果:
(二)使用 switch 语句
1) 编写程序用swith语句实现从键盘读如1,2,3时,屏幕提示不同信息。
public class SwitchTest {
public static void main(String args[])throws java.io.IOException
{char a;
System.out.println("Enter anumber from 1--3:");
a=(char)System.in.read();
switch(a){
case '1':System.out.println("win a Car");break;
case '2':System.out.println("picked the goat");break;
case '3':System.out.println("get to keep your 100");break;
default:System.out.println("entry");
}
}
}
运行结果:
3、使用循环结构
使用for语句
1)
public class TestFor {
public static void main(String args[])throws java.io.IOException{
int fathr,cels;
System.out.println("Celsius Fathrenheit");
for(cels=0;cels<=100;cels+=5)
{
fathr=cels*9/5+32;
System.out.println(cels+""+fathr);}
char a;
outer://this is the label for the outer loop
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{a=(char)System.in.read();
if(a=='b')
break outer;
if(a=='c')
continue outer;
}
}
}
2)改正源程序中错误:
public static void main (String args[]) throws java.io.IOException
运行结果:
使用while语句
1)
/**
* @(#)TestWhile.java
*
*
* @author
* @version 1.00 2013/4/20
*/
import java.io.*;
class TestWhile {
public static void main(String args[])throws java.io.IOException{
char ch;
System.out.println("按 1/2/3 数字键可得大奖!");
System.out.println("按空格键后回车可退回循环操作");
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("真不幸,你没有获得奖品!下次再来吧。");
}
}
}
}
运行结果:
多重循环,输出九九乘法表的程序。
/**
* @(#)TestMul.java
*
*
* @author
* @version 1.00 2013/4/20
*/
public class TestMul {
public static void main(String args[]){
int i,j,n=9;
System.out.print(" * ");
for(i=1;i<=n;i++) System.out.print(" "+i);
System.out.print("\n--------|");
for(i=1;i<=n;i++)
System.out.print("-------");
System.out.println();
for(i=0;i<=n;i++)
{
System.out.print(" "+i+" |");
for(j=1;j<=i;j++)
System.out.print(" "+i*j);
System.out.println();
}
}
}
运行结果: