实验二 Java流程控制语句
一、实验时间和地点
时间:2010年10月18日
地点:电信北楼320判断某年是否为闰年
二、实验内容
1、判断某年月是否为闰年;
2、判断某年月有多少天;
3、有if~else结构对五个数排序;
4、求100~1000内的所有非素数。
三、实验步骤、源程序及实验结果
1、判断某年月是否为闰年
源程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LeapYear {
public static void main(String[] args) throws NumberFormatException, IOException {
int year;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入年份:");
year=Integer.parseInt(input.readLine());
if((year%4==0)&&(year%100==0)||(year%400==0))
System.out.println("是闰年!");
else
System.out.println("不是闰年!");
}
}
运行结果:
2、判断某年月有多少天
源程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MonthDays {
int year;
public void setyear(int year){
this.year=year;
}
public boolean boolyear(){
boolean b=true;
if((year%4==0)&&(year%100==0)||(year%400==0))
b=true;
else
b=false;
return b;
}
public static void main(String[] args) throws NumberFormatException, IOException {
MonthDays md=new MonthDays();
int year,month,days;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入年份:");
year=Integer.parseInt(input.readLine());
System.out.println("请输入月份:");
month=Integer.parseInt(input.readLine());
md.setyear(year);
boolean b=md.boolyear();
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
System.out.println("本月是"+days+"天");
System.out.println("本年是365天");
break;
case 4:
case 6:
case 9:
case 11:
days=30;
System.out.println("本月是"+days+"天");
System.out.println("本年是365天");
case 2:
if(b==true){
days=29;
System.out.println("本月是"+days+"天");
System.out.println("本年是366天");
}
else{
days=28;
System.out.println("本月是"+days+"天");
System.out.println("本年是365天");
}
}
}
}
运行结果:
3、有if~else结构对五个数排序
源程序:
import java.util.Random;
public class paixu {
public static void main(String[] args) {
Random random =new Random();
float a=random.nextFloat();
System.out.println("a="+a);
float b=random.nextFloat();
System.out.println("b="+b);
float c=random.nextFloat();
System.out.println("c="+c);
float d=random.nextFloat();
System.out.println("d="+d);
float e=random.nextFloat();
System.out.println("e="+e);
if(a
if(b if(c if(d else if(c else if(b else if(a else System.out.println("e else if(b if(c else if(d else if(b else if(a System.out.println("a else System.out.println("e else if(a if(c else if(b else if(d System.out.println("a else if(a System.out.println("a else System.out.println("e else//System.out.println("dabc"); if(c else if(b else if(a System.out.println("d else if(d System.out.println("d else System.out.println("e else if(a if(b if(d else if(b else if(c else if(a System.out.println("a else System.out.println("e else if(c if(b else if(d else if(c System.out.println("a else if(a System.out.println("a else System.out.println("e else if(a if(b else if(c System.out.println("a else if(d System.out.println("a else if(a System.out.println("a else System.out.println("e else//System.out.println("dacb"); if(b else if(c System.out.println("d else if(a System.out.println("d else if(d System.out.println("d else System.out.println("e else//System.out.println("cab");//cab if(b if(d else if(b else if(a else if(c System.out.println("c