Java语言程序设计—循环语句

4.1 while循环

while循环在条件为真的情况下,重复的执行语句。
while循环语法如下:
while(循环继续条件){
//循环体
语句(组);
}
注意:循环继续条件应该总是放在圆括号内。只有当循环体制包含一条语句火不包含语句时,循环体的花括号才可以省略。
要保证循环继续条件最终变成false,一便程序能够结束。一个常见的程序设计错误是无限循环。如果程序运行了不寻常的长时间而不结束,可能其中有无限循环。
循环设计策略
第一步:确定需要重复的语句。
第二步:将这些语句放在一个循环中,如下所示:
while(true){
语句组;
}
第三步:为循环继续条件编码,并为控制循环添加适合的语句。如下所示:
while(循环继续条件){
语句组;
用于控制循环的附件语句;
}
注意:在循环控制中,不能使用浮点直来比较是否相等。因为浮点值都是某些值得近似值,使用他们可能导致不精确的循环次数和不精确的结果。

4.2 do-while循环

要点提示:do-while循环和while循环基本一样,不同的是它先执行循环体一次,然后判断循环继续条件。
do-while循环是while循环的变体。它的语法如下:
do{
//循环体;
语句(组);
}while(循环继续条件);
在这里插入图片描述
提示:如果循环中的语句至少需要执行一次,建议使用do-while循环。

4.3 for循环

要点提示:for循环具有编写循环的简明语法。
for循环的语法如下:
for(初始操作;循环继续条件;每次迭代后的操作){
//循环体;
语句(组);
}
在这里插入图片描述
for循环语句从关键字for开始,然后使用双括号括住的循环控制结构体。

4.4 嵌套循环

嵌套循环是有一个外层循环和一个或多个内层循环组成的。每当重复执行一次外层循环时将再次进行内部循环,然后重新开始。
注意:嵌套循环将执行的时间较长。

4.5 最小化数值错误

要点提示:在循环继续条件中使用浮点数将导致数值错误。
涉及浮点数的数值误差是不可避免的,因为浮点数在计算机中本省就是近似表示的。

4.6 break和continue

要点提示:关键字break和continue在循环中提供了额外的控制。
注意:continue语句总是在一个循环内。在while和do-while循环中,continue语句之后会马上计算循环继续条件;而在for循环中,continue语句之后会立即先执行每次迭代后的动作,在计算循环继续条件。

编写程序

4-11

import java.util.*;
public class day4_11 {
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
		System.out.print("输入一个值:");
		int number=scanner.nextInt();
		double a=1;
		long b=1;
		for(int i=1;i<=number;i++){
			 b=i*b;
			double c=1.0/b;
			a=a+c;
		}
		System.out.println(a);
	}
}

4-12

import java.util.*;
public class day4_12 {
	public static void main(String args[]){
	Scanner scanner=new Scanner(System.in);
	System.out.print("输入年份:");
	int year=scanner.nextInt();
	System.out.print("输入一月份第一天星期:");
	int match=scanner.nextInt();
	String array[]=new String[]{"A","B","C","D","E","F","G"}; 
	if(year%4==0&&year%100!=0||year%400==0){
		switch(0){
		case 0: System.out.println(year+"  1月"+array[match]);
		case 1: System.out.println(year+"  2月"+(array[(match+3)%7]));
		case 2: System.out.println(year+"  3月"+(array[(match+4)%7]));
		case 3: System.out.println(year+"  4月"+array[match%7]);
		case 4: System.out.println(year+"  5月"+(array[(match+2)%7]));
		case 5: System.out.println(year+"  6月"+(array[(match+5)%7]));
		case 6: System.out.println(year+"  7月"+(array[(match+7)%7]));
		case 7: System.out.println(year+"  8月"+(array[(match+3)%7]));
		case 8: System.out.println(year+"  9月"+(array[(match+6)%7]));
		case 9: System.out.println(year+"  10月"+(array[(match+1)%7]));
		case 10: System.out.println(year+"  11月"+(array[(match+4)%7]));
		case 11: System.out.println(year+"  12月"+(array[(match+6)%7]));
		}}else{
			switch(0){
			case 0: System.out.println(year+"  1月"+array[match]);
			case 1: System.out.println(year+"  2月"+(array[(match+3)%7]));
			case 2: System.out.println(year+"  3月"+(array[(match+3)%7]));
			case 3: System.out.println(year+"  4月"+(array[(match+6)%7]));
			case 4: System.out.println(year+"  5月"+(array[(match+1)%7]));
			case 5: System.out.println(year+"  6月"+(array[(match+4)%7]));
			case 6: System.out.println(year+"  7月"+(array[(match+6)%7]));
			case 7: System.out.println(year+"  8月"+(array[(match+2)%7]));
			case 8: System.out.println(year+"  9月"+(array[(match+6)%7]));
			case 9: System.out.println(year+"  10月"+(array[(match+7)%7]));
			case 10: System.out.println(year+"  11月"+(array[(match+3)%7]));
			case 11: System.out.println(year+"  12月"+(array[(match+5)%7]));
			}
		}	
	}
}

4-16

import java.util.*;
public class d4_16 {
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
		int pcount=0,ccount=0;
		while(true){
		System.out.print("请输入数字:");
		int p=scanner.nextInt();
		int c=(int)(Math.random()*3);
		if(p==c){
			System.out.print("平局");
		}if(p==0&&c==1){
			System.out.println("电脑胜");
			ccount++;
		}if(p==0&&c==2){
			System.out.println("玩家胜");
			pcount++;
		}if(p==1&&c==2){
			System.out.println("电脑胜");
			ccount++;
		}if(p==1&&c==0){
			System.out.println("玩家胜");
			pcount++;
		}if(p==2&&c==0){
			System.out.println("电脑胜");
			ccount++;
		}if(p==2&&c==1){
			System.out.println("玩家胜");
			pcount++;
		}if(ccount==2||pcount==2){
				break;
		}}
	if(pcount==2){
		System.out.println("玩家胜");
	}else{
		System.out.println("电脑胜");
		}
	}
}
}

4-18

import java.util.*;
public class day4_18 {
	public static void main(String args[]){
		Scanner scanner=new Scanner(System.in);
		System.out.print("输入数值:");
		int num=scanner.nextInt();
		String a="";
		while(true){
			int number=num%16;
			
			if(number>9){
				switch(number){
				case 10:a="A"+a;break;
				case 11:a="B"+a;break;
				case 12:a="C"+a;break;
				case 13:a="D"+a;break;
				case 14:a="E"+a;break;
				case 15:a="F"+a;break;
				}
			}else{
			
			 a=number+a;
			}
			num/=16;
			if(number==0){
				break;
			}
		}
		System.out.println("0X"+a);
	}

}

猜你喜欢

转载自blog.csdn.net/SQLserver2008gbg/article/details/83216027