catalogue
6, Special statements in a loop
Use Scanner to read string / integer / floating point number
Use the Scanner loop to read N numbers
Logical control is an indispensable part of all the program world. In fact, it is the same in life (choose primary school, choose high school, choose university, choose object, choose public entrance examination, postgraduate entrance examination, go abroad or work directly...) life is full of choices everywhere. Sometimes the results are not satisfactory. Don't lose heart and give up, because the opportunity is uncertain. It's waiting for you at the corner of the next choice~
In fact, the world of Java is inseparable from the choice of logic control. Next, let's take you to some basic logic control statements in Kangkang Java.
1, if statement
1. Syntax form of if statement:
if(Boolean expression){ //Execute code when conditions are met }
if(Boolean expression){ //Execute code when conditions are met }else{ //Execute code when conditions are not met }
if(Boolean expression){ //Execute code when conditions are met }else if(Boolean expression){ //Execute code when conditions are met }else{ //Execute code when none of the conditions are met }
After speaking so much grammar, let's give an example.
Judge whether a year is a leap year as follows:
int year = 2000; if (year % 100 == 0) { // Determine whether it is a leap year of the century if (year % 400 == 0) { System.out.println("It's a leap year"); } else { System.out.println("Not a leap year"); } } else { // Determine whether it is an ordinary leap year if (year % 4 == 0) { System.out.println("It's a leap year"); Bit technology } else { System.out.println("Not a leap year"); } }
be careful:
int x = 10; int y = 10; if (x == 10) if (y == 10) System.out.println("aaa"); else System.out.println("bbb"); //Because else matches the nearest if, the output is aaa
2, switch statement
switch( ){ case Content 1 : { Execute statement when content is satisfied; break; } case Content 2 : { Execute statement when content is satisfied; break; } ... default:{ Execute the statement when the content is not satisfied; break; } }
int day = 1; switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Incorrect input"); break; }
2. The value in switch can only be int / byte / short / enum / char / String.
long float double boolean cannot be used as a switch parameter.
3, while loop
Basic syntax format:
while(Cycle condition){ Circular statement; }
int n = 1; int result = 1; while (n <= 5) { result *= n; //This step is equivalent to result=n*result n++; } System.out.println(num); // The execution result is 120
2. Calculate 1+ 2! + 3! + 4! + 5!
int num = 1; int sum = 0; while (num <= 5) { // The outer loop is responsible for finding the sum of factorials int factorResult = 1; int tmp = 1; while (tmp <= num) { // The inner loop is responsible for finding the value of each factorial factorResult *= tmp; tmp++; } sum += factorResult; num++; } System.out.println("sum = " + sum);
4, for loop
for(Expression 1;Expression 2;Expression 3){ Circulatory body; }
int result = 0; for (int i = 1; i <= 5; i++) { result *= i; } System.out.println("result = " + result);
Example 2: calculation 1+ 2! + 3! + 4! + 5!
int sum = 0; for (int i = 1; i <= 5; i++) { //The number of external circulation is five as required int tmp = 1; for (int j = 1; j <= i; j++) { //Find the factorial of each number in the inner loop tmp *= j; } sum += tmp; } System.out.println("sum = " + sum);
(error prone point of this question: when defining tmp, remember to put it outside the inner loop and in the outer loop. If it is placed outside the outer loop, tmp will only be initialized once, and there will be problems in subsequent factorization, so we can't get the desired results.)
5, do...while loop
Basic syntax format:
do{ Circular statement; }while(Cycle condition);
int num = 1; do { System.out.println(num); num++; } while (num <= 10);
Note: don't forget the semicolon at the end of the do...while loop. Generally, do while uses less, while and for loops use the most.
6, Special statements in a loop
1. break statement
Example: find 100~ The first number in 200 is a multiple of 3 and printed.
int num = 100; while (num <= 200) { if (num % 3 == 0) { System.out.println("A multiple of 3 was found, by:" + num); break; } num++; } // The execution result found a multiple of 3: 102
This shows that break can jump out of the loop. If there are nested loops, break will jump out of the nearest loop.
2. continue statement
int num = 100; while (num <= 200) { if (num % 3 != 0) { num++; continue; } System.out.println("A multiple of 3 was found, by:" + num); num++; }
7, Input and output in Java
1. Output
This is also mentioned in the first section of getting to know Java. There are three kinds of output in Java.
2,System.out.print("hello world"); // Output a string without line breaks
3, int a=77;
System.out.printf("a=%d\n",a); // As in c, format the output.

2. Input
Use Scanner to read string / integer / floating point number
import java.util.Scanner; // When using Scanner, you need to import util package Scanner sc = new Scanner(System.in); System.out.println("Please enter your name:"); String name = sc.nextLine(); //Read a string System.out.println("Please enter your age:"); int age = sc.nextInt(); //Read an integer System.out.println("Please enter your salary:"); float salary = sc.nextFloat(); //Read a floating point number System.out.println("Your information is as follows:"); System.out.println("full name: "+name+"\n"+"Age:"+age+"\n"+"Salary:"+salary); // results of enforcement Please enter your name: Zhang San Please enter your age: 18 Please enter your salary: 1000 Your information is as follows: full name: Zhang San Age: 18 Salary: 1000.0
Of course, in addition to the above, data types such as nextBoolean (), nextByte (), nextShort () are also supported.
Use the Scanner loop to read N numbers

How can it end?
Guess numbers games
Finally, write a guessing game to end this article~
Expand the usage of random (also import util package before use)——

import java.util.Random; import java.util.Scanner; class Test { public static void main(String[] args) { Random random = new Random(); Scanner sc = new Scanner(System.in); int guess = random.nextInt(100); while (true) { System.out.println("Please enter the number you want to enter: (1-100)"); int num = sc.nextInt(); if (num < guess) { System.out.println("Low"); } else if (num > guess) { System.out.println("High"); } else { System.out.println("You guessed right"); break; } } } }
The final running result is:
Isn't it very interesting? After learning the loop and logic, you can write more interesting games.
The next article is about the use of methods (functions). I really think I'm so slow to write. Hahaha
to be continue →
Welcome to discuss and exchange with each other. Welcome to catch insects!