catalogue
2, Circular application example
2. Randomly generate four digit verification code
4. Find prime numbers within 1-1000
5. Judge the day of a month in a year as the first day of the year
6. Student achievement management
preface
The meaning of using circular statements: under certain conditions, repeat a certain program to make the program concise. Today, let's learn the basic structure and application examples of while, do while and for loops in Java!
1, Basic structure
1.while loop
While (conditional expression){
Circulatory body
}
example
public void test() { int n = 0; while(n<10) { System.out.print(n+" "); n++; } }
The compiled results of the instance are shown in the following figure:
2.do while loop
do{
Circulatory body
}While (conditional expression);
example
public void test(){ int a = 0; do{ System.out.print(a+"\t"); a++; }while(a<10); }
The compiled results of the instance are shown in the following figure:
3.for loop
For (parameter initialization; loop condition; change loop variable){
Circulatory body
}
example
public void test() { for(int n=1;n<10;n++) { System.out.print(n+"\t"); } }
The compiled results of the instance are shown in the following figure:
2, Circular application example
1. Rolling phase division
public void testGongYue() { int a = 78; int b = 36; while(true) { int n = a % b; if(n == 0) { System.out.println("The maximum common divisor is:"+b); break; } a=b; b=n; } }
2. Randomly generate four digit verification code
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. Print triangles
(1) Right triangle
public void test() { for(int i=0;i<5;i++) { for(int j=0;j<=i;j++) { System.out.print("*"); } System.out.println(); } }
(2) Isosceles triangle
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) Christmas tree
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++) {//5th floor Christmas tree 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. Find prime numbers within 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"); } } }
Screenshot of operation results:
5. Judge the day of a month in a year as the first day of the year
public void daysTest() { Scanner sc = new Scanner(System.in); System.out.print("Please enter the year:"); int year = sc.nextInt(); System.out.print("Please enter month:"); int month = sc.nextInt(); System.out.print("Please enter the day:"); int day = sc.nextInt(); /* * Is it a leap year */ 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+"It's the third time this year"+days+"day"); }
6. Student achievement management
Enter the student's score in A circle. If the student's score is 90 ~ 100, it is A, 80 ~ 89 is B, 60 ~ 79 is C, and below 60 points is A failure.
Coding realizes the following functions:
- Count the total scores of students
- Count the average score of students
- Count the number of students with A, B, C and failed grades
- Calculate the proportion of A, B, C and D in the total number of examinees.
public void test() { double sum = 0 ; //Total score double average = 0;//Average score int count1 = 0,count2 = 0,count3 = 0,count4 = 0;//Number of people at each grade int p = 0;//Total number of examinees double r1 = 0,r2 = 0,r3 = 0,r4 = 0;//Ratio of grades Scanner sc = new Scanner(System.in); boolean flag = true; while (flag) { System.out.print("Please enter the student's grade:"); double score = Double.parseDouble(sc.nextLine()); p += 1; if (score >= 90 && score <= 100) { //System.out.println("the student's grade is A!"); count1 += 1; } else if (score >= 80 && score <= 89) { //System.out.println("the student's grade is B!"); count2 += 1; } else if (score >= 60 && score <= 79) { //System.out.println("the student's grade is C!"); count3 += 1; } else { //System.out.println("the student failed!"); 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("End, please enter-1"); int x = Integer.parseInt(sc.nextLine()); if (x != -1) { break; } else { flag = false; System.out.println("Exited!"); //System.out.println("your input is illegal!"); } } } System.out.println("The students' total score is:"+sum); System.out.println("The average score of the students is:"+average); System.out.println("The result is A Number of students waiting:"+count1+","+"The result is B Number of students waiting:"+count2+"," +"The result is C Number of students waiting:"+count3+","+"Number of failed grades:"+count4); System.out.println("achievement A Etc. in the total number"+r1); System.out.println("achievement B Etc. in the total number"+r2); System.out.println("achievement C Etc. in the total number"+r3); System.out.println("Of the total number of people, such as failing grades"+r4); }
Screenshot of operation results:
7. Small computer system
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.addition\t 2.reduce method\t "); System.out.println("3.multiplication\t 4.except method\t "); System.out.println("5.power \t 6.Take mold\t "); System.out.println("7.open square\t 8.is even \t "); System.out.println("9.Maximum\t 10.Sort from large to small\t "); System.out.println("11.an isosceles triangle\t 12.multiplication table\t "); System.out.println("0.sign out"); System.out.println("-------------------------------"); System.out.println("Please select operation mode"); String choice = sc.nextLine(); switch (choice) { case EXIT: f = false; System.out.println("The system is exiting, please wait!"); for (int i = 0; i < 10; i++) { System.out.print("."); Thread.sleep(100); } System.out.println("\n The system has exited,Thank you for using!"); break; case PLUS: System.out.println("please enter a number a:"); a = Integer.parseInt(sc.nextLine()); System.out.println("please enter a number b:"); b = Integer.parseInt(sc.nextLine()); int sum = a + b; System.out.println(a + "+" + b + "=" + sum); break; case SUB: System.out.println("please enter a number a:"); a = Integer.parseInt(sc.nextLine()); System.out.println("please enter a number b:"); b = Integer.parseInt(sc.nextLine()); int sub = a - b; System.out.println(a + "-" + b + "=" + sub); break; case MUL: System.out.println("please enter a number am:"); a = Integer.parseInt(sc.nextLine()); System.out.println("please enter a number b:"); b = Integer.parseInt(sc.nextLine()); int mul = a * b; System.out.println(a + "*" + b + "=" + mul); break; case DIVI: System.out.println("Please enter divisor x:"); x = Double.parseDouble(sc.nextLine()); System.out.println("Please enter the divisor y:"); y = Double.parseDouble(sc.nextLine()); if (y != 0) { double d = x / y; System.out.println(x + "➗" + y + "=" + d); } else { System.out.println("Divisor cannot be 0"); } break; case POW: System.out.println("please enter a number x:"); x = Double.parseDouble(sc.nextLine()); System.out.println("please enter a number 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("please enter a number x:"); x = Double.parseDouble(sc.nextLine()); System.out.println("please enter a number y:"); y = Double.parseDouble(sc.nextLine()); double result1 = x % y; System.out.println(x + "%" + y + "=" + result1); break; case SQRT: System.out.println("please enter a number x:"); x = Double.parseDouble(sc.nextLine()); double result2 = Math.sqrt(x); System.out.println(x + "Result of prescription:" + result2); break; case EVEN: System.out.println("please enter a number a:"); a = Integer.parseInt(sc.nextLine()); if (a % EVEN_FLAG == 0) { System.out.println(a + "It's an even number"); } else { System.out.println(a + "Not even"); } break; case MAX: System.out.println("please enter a number a:"); a = Integer.parseInt(sc.nextLine()); System.out.println("please enter a number b:"); b = Integer.parseInt(sc.nextLine()); // int max = Math.max(a, b); System.out.println(a + "and" + b + "The maximum value between is:" + Math.max(a, b)); break; case SORT_MAX_TO_MIN: System.out.println("please enter a number a:"); a = Integer.parseInt(sc.nextLine()); System.out.println("please enter a number b:"); b = Integer.parseInt(sc.nextLine()); int max = Math.max(a, b); int min = Math.min(a, b); System.out.println("Sort from large to small as:" + max + " ," + min); break; case TRIANGLE: System.out.print("Please enter the number of layers to print the triangle:"); 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("Illegal input, please use Y or 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("The system is exiting, please wait!"); for (int i = 0; i < 10; i++) { System.out.print("."); Thread.sleep(100); } System.out.println("\n The system has exited,Thank you for using!"); } else { System.out.println("Please re-enter Y[continue]or N[end]: "); } } } } }
Screenshot of operation results:
8. Dice game
/** * 3 Six sieves are thrown together, and the program ends * 1 Free 1-5 times 2 yuan 6-10 times 20 yuan more than 10 times 40 yuan * @throws InterruptedException */ public void looptest1() throws InterruptedException { Random ran = new Random(); int count = 0; double money = 12500.00; int win = 0;//Number of winners int lose = 0;//Number of losers 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("The first"+(i+1)+"The game is over!"); System.out.println("The first"+(i+1)+"People throw" + count + "Secondary sieve"); double p = 1.0 / count * 100; System.out.println("The first"+(i+1)+"The probability of winning the prize is:" + p); money += 3000; System.out.println("The first"+(i+1)+"The person balance is:" + money); if(money >= 10000) { win +=1; }else { lose +=1; } break; } } } System.out.println("The number of winners is:"+win); System.out.println("The number of losers is:"+lose); }
Screenshot of operation results: