Java基础–循环语句及应用

前言

一、基本结构

1.while循环

2.do while循环

3.for循环

二、循环应用实例

1.辗转相除法

2.随机产生四位验证码

         3.打印三角形

    (1)直角三角形

    (2)等腰三角形

      (3)   圣诞树

4.找出1-1000以内的素数

         5.判断某年某月某天是该年的第多少天

         6.学生成绩管理

         ​7.小型计算机系统

​         8.掷色子游戏

前言

        使用循环语句的意义:在一定条件下,重复执行某段程序,使得程序变得简洁。今天,一同学习while、do while及for循环在Java中的基本结构和应用实例吧!

一、基本结构

1.while循环

while(条件表达式){

        循环体

}

实例

public void test() {
		int n = 0;
		while(n<10) {
			System.out.print(n+"  ");
			n++;
		}
	}

实例编译后的结果如下图所示:

2.do while循环

do{

        循环体

}while(条件表达式);

实例

public void test(){
    int a = 0;
    do{
        System.out.print(a+"t");
        a++;
    }while(a<10);
}

实例编译后的结果如下图所示:

3.for循环

for(参数初始化;循环条件;改变循环变量){

        循环体

}

实例

public void test() {
		for(int n=1;n<10;n++) {
			System.out.print(n+"t");
		}
	}

实例编译后的结果如下图所示:、

二、循环应用实例

1.辗转相除法

public void testGongYue() {
	int a = 78;
	int b = 36;
	while(true) {
		int n = a % b;
		if(n == 0) {
			System.out.println("最大公约数为:"+b);
			break;
		}
		a=b;
		b=n;
	}
}

2.随机产生四位验证码

public void RandomTest() {
	Random ran = new Random();
	String s = "";
	int count = 0;
	while(count<4) {
		char c = (char) (ran.nextInt('y'-'0')+'0');
		s += c;
		count++;
	}
	System.out.println(s);
}

3.打印三角形

(1)直角三角形

public void test() {
	for(int i=0;i<5;i++) {
		for(int j=0;j<=i;j++) {
			System.out.print("*");
		}
	System.out.println();	
	}
}

(2)等腰三角形

public void testLoop() {
		int SIZE = 6;
		for (int h = 1; h <= SIZE; h++) {
			
			for(int k=1;k<=SIZE-h;k++) {
				System.out.print(" ");
			}
			
			for(int i=1;i<=2*h-1;i++) {
				System.out.print("*");
			}
			System.out.println();
		}
}

(3) 圣诞树

public void ChristmasTree() {
	int SIZE = 5;
	for (int h = 1; h <= SIZE; h++) {
			
		for(int k=1;k<=SIZE-h;k++) {
			System.out.print(" ");
		}
			
		for(int i=1;i<=2*h-1;i++) {
			System.out.print("*");
		}
		System.out.println();
	}
		
	for(int t=1;t<=4;t++) {//5层圣诞树  f=2*5-1
			
		for(int f=1;f<=9;f++) {
			if((t==1&&f==2) || (t==1&&f==8)) {
				System.out.print("$");
			}else if(f>=4 && f<=6) {
				System.out.print("|");
			}else {
				System.out.print(" ");
			}	
		}
		System.out.println();
	}
}

4.找出1-1000以内的素数

public void test() {
	for (int i = 1; i < 1000; i++) {
		boolean flag = true;
		for (int j = 2; j < i; j++) {
			flag = false;
			if(i%j == 0) {	
				flag = true;
				break;
			}	
		}
		if(flag == false) {
			System.out.print(i+"t");
		}
	}
}

运行结果部分截图:

 5.判断某年某月某天是该年的第多少天

public void daysTest() {
    Scanner sc = new Scanner(System.in);
    System.out.print("请输入年份:");
	int year = sc.nextInt();

	System.out.print("请输入月份:");
	int month = sc.nextInt();

	System.out.print("请输入日子:");
	int day = sc.nextInt();
	/*
	* 是否为闰年
	*/
	boolean isLeap = (year % 400 == 0) || year % 4 == 0 && year % 100 != 0;
		
	int days = 0;
	for(int m =1; m <month;m++) {	
		if(m==1||m==3||m==5|m==7||m==8||m==10||m==12) {
			days+=31;
		}else if(m==2) {
			days+=28;
		}else {
			days+=30;
		}
	}
	days+=day;
	if(month>2 &&  isLeap) {
		days+=1;
	}	
	System.out.println(year+"-"+month+"-"+day+"是今年的第"+days+"天");
}

6.学生成绩管理

循环输入学生成绩,如果学生成绩在90~100为 A, 80~89为B, 60~79为C,60分以下为不及格。
           编码实现如下功能:
           - 统计学生的总成绩
           - 统计学生的平均成绩
           - 统计成绩为A,B,C,和不及格人数
           - 计算A,B,C,D人数占总考试人数的比例。

public void test() {
		
		double sum = 0 ; //总成绩
		double average = 0;//平均成绩
		int count1 = 0,count2 = 0,count3 = 0,count4 = 0;//各成绩等级的人数
		int p = 0;//考试总人数
		double r1 = 0,r2 = 0,r3 = 0,r4 = 0;//各成绩等级的比率
		
		Scanner sc = new Scanner(System.in);
		
		boolean flag = true;
		
		while (flag) {
			System.out.print("请输入学生的成绩:");
			double score = Double.parseDouble(sc.nextLine());
			p += 1;
			
			if (score >= 90 && score <= 100) {
				//System.out.println("该同学成绩为A!");
				count1 += 1;
			} else if (score >= 80 && score <= 89) {
				//System.out.println("该同学成绩为B!");
				count2 += 1;
			} else if (score >= 60 && score <= 79) {
				//System.out.println("该同学成绩为C!");
				count3 += 1;
			} else {
				//System.out.println("该同学成绩不及格!");
				count4 += 1;
			}
			sum += score;
			average = sum/score;
			r1 = (double)count1/p;
			r2 = (double)count2/p;
			r3 = (double)count3/p;
			r4 = (double)count4/p;
			
			while (flag) {
				System.out.println("结束请输入-1");
				int x = Integer.parseInt(sc.nextLine());
				if (x != -1) {
					break;	
				} else {
					flag = false;
					System.out.println("已退出!");
					//System.out.println("你的输入不合法!");
				}
			}
		}
		System.out.println("同学们的总成绩为:"+sum);
		System.out.println("同学们的平均成绩为:"+average);
		System.out.println("成绩为A等的同学人数:"+count1+","+"成绩为B等的同学人数:"+count2+","
				+"成绩为C等的同学人数:"+count3+","+"成绩不及格的人数:"+count4);
		System.out.println("成绩A等占总人数的"+r1);
		System.out.println("成绩B等占总人数的"+r2);
		System.out.println("成绩C等占总人数的"+r3);
		System.out.println("成绩不及格等占总人数的"+r4);
		
	}

运行结果部分截图:

​7.小型计算机系统

public void demo1() throws InterruptedException {
		Scanner  sc = new Scanner(System.in);
		
		final String PLUS = "1";
		final String SUB = "2";
		final String MUL = "3";
		final String DIVI = "4";
		final String POW = "5";
		final String MOD = "6";
		final String SQRT = "7";
		final String EVEN = "8";
		final String MAX = "9";
		final String SORT_MAX_TO_MIN = "10";
		final String TRIANGLE = "11";
		final String NINE_TIMES_NINE = "12"; 
		final String EXIT = "0";
		
		final int EVEN_FLAG = 2;
	
		boolean f = true;
		int a = 0,b = 0;
		double x = 0,y =0 ;
		
		while (f) {
			System.out.println("-------------------------------");
			System.out.println("1.加法t     2.减    法t  ");
			System.out.println("3.乘法t     4.除    法t  ");
			System.out.println("5.幂     t     6.取模t  ");
			System.out.println("7.开    方t    8.是否为偶数t  ");
			System.out.println("9.最大值t    10.从大至小排序t  ");
			System.out.println("11.等腰三角形t    12.九九乘法表t  ");
			System.out.println("0.退出");
			System.out.println("-------------------------------");

			System.out.println("请选择运算方式");
			String choice = sc.nextLine();

			switch (choice) {
			case EXIT:
				f = false;
				System.out.println("系统正在退出中,请稍候!");
				for (int i = 0; i < 10; i++) {
					System.out.print(".");
					Thread.sleep(100);
				}
				System.out.println("n系统已退出,谢谢使用!");
				break;
			case PLUS:
				System.out.println("请输入数字a:");
				a = Integer.parseInt(sc.nextLine());
				System.out.println("请输入数字b:");
				b = Integer.parseInt(sc.nextLine());
				int sum = a + b;
				System.out.println(a + "+" + b + "=" + sum);
				break;
			case SUB:
				System.out.println("请输入数字a:");
				a = Integer.parseInt(sc.nextLine());
				System.out.println("请输入数字b:");
				b = Integer.parseInt(sc.nextLine());
				int sub = a - b;
				System.out.println(a + "-" + b + "=" + sub);
				break;
			case MUL:
				System.out.println("请输入数字am:");
				a = Integer.parseInt(sc.nextLine());
				System.out.println("请输入数字b:");
				b = Integer.parseInt(sc.nextLine());
				int mul = a * b;
				System.out.println(a + "*" + b + "=" + mul);
				break;
			case DIVI:
				System.out.println("请输入除数x:");
				x = Double.parseDouble(sc.nextLine());
				System.out.println("请输入被除数y:");
				y = Double.parseDouble(sc.nextLine());
				if (y != 0) {
					double d = x / y;
					System.out.println(x + "➗" + y + "=" + d);
				} else {
					System.out.println("被除数不能为0");
				}
				break;
			case POW:
				System.out.println("请输入数字x:");
				x = Double.parseDouble(sc.nextLine());
				System.out.println("请输入数字y:");
				y = Double.parseDouble(sc.nextLine());
				int result = (int) Math.pow(x, y);
				System.out.println(x + "^" + y + "=" + result);
				break;
			case MOD:
				System.out.println("请输入数字x:");
				x = Double.parseDouble(sc.nextLine());
				System.out.println("请输入数字y:");
				y = Double.parseDouble(sc.nextLine());
				double result1 = x % y;
				System.out.println(x + "%" + y + "=" + result1);
				break;
			case SQRT:
				System.out.println("请输入数字x:");
				x = Double.parseDouble(sc.nextLine());
				double result2 = Math.sqrt(x);
				System.out.println(x + "开方的结果:" + result2);
				break;
			case EVEN:
				System.out.println("请输入数字a:");
				a = Integer.parseInt(sc.nextLine());
				if (a % EVEN_FLAG == 0) {
					System.out.println(a + "是偶数");
				} else {
					System.out.println(a + "不是偶数");
				}
				break;
			case MAX:
				System.out.println("请输入数字a:");
				a = Integer.parseInt(sc.nextLine());
				System.out.println("请输入数字b:");
				b = Integer.parseInt(sc.nextLine());
				// int max = Math.max(a, b);
				System.out.println(a + "和" + b + "之间的最大值为:" + Math.max(a, b));
				break;
			case SORT_MAX_TO_MIN:
				System.out.println("请输入数字a:");
				a = Integer.parseInt(sc.nextLine());
				System.out.println("请输入数字b:");
				b = Integer.parseInt(sc.nextLine());
				int max = Math.max(a, b);
				int min = Math.min(a, b);
				System.out.println("由大到小排序为:" + max + " ," + min);
				break;
			case TRIANGLE:
				System.out.print("请输入打印三角形的层数:");
				int size = Integer.parseInt(sc.nextLine());
				for (int h = 1; h <= size; h++) {
					for (int j = 1; j <= size - h; j++) {
						System.out.print(" ");
					}

					for (int i = 1; i <= 2 * h - 1; i++) {
						System.out.print("*");
					}
					System.out.println();
				}
				break;
			case NINE_TIMES_NINE:
				for (int i = 1; i < 10; i++) {
					for (int j = 1; j <= i; j++) {
						System.out.print(j + "*" + i + "=" + (i * j) + "t");
					}
					System.out.println();
				}
				break;
			default:
				boolean flag = true;
				while (flag) {
					System.out.println("输入不合法,请使用Y或N:");
					String s = sc.nextLine();
					if ("Y".equals(s) || "y".equals(s)) {
						break;
					} else if ("N".equals(s) || "n".equals(s)) {
						flag=false;
						System.out.println("系统正在退出中,请稍候!");
						for (int i = 0; i < 10; i++) {
							System.out.print(".");
							Thread.sleep(100);
						}
						System.out.println("n系统已退出,谢谢使用!");
					} else {
						System.out.println("请重新输入Y[继续]或N[结束]:");
					}
				}	
			}
		}
		
	}

运行结果部分截图:

​ 8.掷色子游戏

    /**
	 * 3个筛子共同掷出6,程序结束
	 * 1次免费   1-5次 2块   6-10次 20块   10次以上 40块
	 * @throws InterruptedException 
	 */
public void looptest1() throws InterruptedException {
		Random ran = new Random();
		int count = 0;
		double money = 12500.00;
		
		int win = 0;//赢人数
		int lose = 0;//输人数
		
		for (int i = 0; i < 10; i++) {
			while (true) {
				int a = ran.nextInt(6) + 1; //ran.nextInt(n) --> [0,n)
				int b = ran.nextInt(6) + 1;
				int c = ran.nextInt(6) + 1;
				//System.out.println("[" + a + "," + b + "," + c + "]");
				count++;
				if (count == 1) {
					money -= 0;
				} else if (count <= 5) {
					money -= 2;
				} else if (count <= 10) {
					money -= 20;
				} else {
					money -= 40;
				}

				//Thread.sleep(100);
				if (a == 6 && b == 6 && c == 6) {
					System.out.println("第"+(i+1)+"人游戏结束!");
					System.out.println("第"+(i+1)+"人共掷出" + count + "次筛子");
					double p = 1.0 / count * 100;
					System.out.println("第"+(i+1)+"人中奖概率为:" + p);
					money += 3000;
					System.out.println("第"+(i+1)+"人余额为:" + money);
					if(money >= 10000) {
						win +=1;
					}else {
						lose +=1;
					}
					break;
				}
			} 
		}
		System.out.println("赢的人数为:"+win);
		System.out.println("输的人数为:"+lose);
	}

 运行结果部分截图:

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>