java loop statement (personal note six)

Lession5 process statement 


1 while loop   

while(){ //An expression that can return true or false after the conditional expression holds the line
      //execute statement
}

Special emphasis is never placed on semicolons => while() ;


//example int age=1; while loop infinite loop
while(age<=10){
System.out.println("Learn "+age+"year");
age++;

}


//Example while loop outputs 365 days in a year
int day=1;
while(day<=365)
{
System.out.println("第"+day+"天");
++day;

}


//Example input a number, if the number is 8888, end, if not, print it out, continue to input
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int num =scan.nextInt(); //Enter a number from the keyboard
while(num!= 8888)
{
System.out.println("The number you entered is "+num);
System.out.println("The input is wrong, please input again");
num=scan.nextInt(); //Enter again
}
System.out.println("The input is correct, the program ends");
scan.close();
}
}

//Example infinite loop Standard infinite loop
while(true) //this is an infinite loop
{
System.out.println("I want to kill the little Japanese");
}

//For the code written after the infinite loop, the compilation will fail because it cannot be executed until
System.out.println("Do I still have a chance to execute it"); //Error Unreachable code

//Example jumps out of the infinite loop
int a=10;
while(true)
{
System.out.println("I want to kill the little Japanese");
a++;
if(a>100){
break; // break out of the loop
}
}
System.out.println("Do I still have a chance to execute it"); //reachable

 2 do while

while(conditional expression) {

       loop body

}

//this is the do while loop
do{
loop body
}
while(conditional statement);  
Difference from while loop

do while will execute the contents of the loop body first (regardless of 3721)


//Example output 12 months
  int month=0;
do{
month++;
System.out.println("Currently is "+month+"month");
}

while(month<12);


//Example, enter a number, if the number is 8888, end, if not, print it out, continue to enter
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
   int num=0; //The variable must be initialized before use The local variable num may not have been initialized
   Scanner scan=new Scanner(System.in);
   do {
   num=scan.nextInt();
   System.out.println("The number you entered is "+num);  
   }
   while(num!=8888); 
   System.out.println("The input is correct, the program ends");
   scan.close(); 
}
}

//Example, randomly generate 10 numbers, the value is between 1-9, and pick the largest one.
1) Random number generation method
// The class under the java.lang package, when used, you can use it without the need to guide the package
//Math.random() can generate a random number, which is a decimal with a value between 0 (inclusive) and 1 (exclusive)
//double num=Math.random(); //Generate a random decimal between 0-1
double num=(int)(Math.random()*10); //Pay attention to the priority, the extension cannot be less
System.out.println(num); 

2) The subject
public class Test {
public static void main(String[] args) {
    int max=0;
int i=0;
do{
int num=  -(int) (Math.random()*10);
if(i==0) { //If v is the first loop, directly assign the generated number to max
max=num;
}
if(max<num){
max=num;
}
System.out.print(num+"\t");
i++;
}
while(i<10); 
System.out.println("The largest number is "+max);
}
}

 3 for loop

for(initialization expression; conditional expression; operation expression after loop) {
  //loop body
}

// Example 1
for(int i=0; i<10;i++){
System.out.println("hello world"+"第"+(i+1)+"遍");
}

//1 Execute the initialization expression, only once
//2 execute the conditional expression
//3 If the condition is true, execute the loop body, if the condition is not true, the loop ends
//4 After executing the loop body, execute the execution statement after the loop

// Example 2
int i=0; 
for(System.out.println("Haha, how can I just initialize like this?");i<3; System.out.println("This is what I'm going to do after the loop")){
System.out.println("This is the body of the loop");
}
Compilation error, the second expression must be a conditional expression (can return true or false) //System.out.println("This is my conditional expression"); error


//Example 3 This example can be compiled and passed
int i=0;
for(System.out.println("Haha, how can I just initialize like this?"); i<3; System.out.println("This is what I'm going to do after the loop")){
i++;
System.out.println("This is the body of the loop");
}
result:
Haha, why can't I just initialize it like this?
This is the body of the loop
This is what I do after the loop
This is the body of the loop
This is what I do after the loop
This is the body of the loop
This is what I do after the loop

//Example 4 An infinite loop written with for
   for(;;){
 //The operation makes the conditional expression always true
}
 
//Example 5 outputs 0-99 
int i;
for(i=0;i<100;i++) { //If it is written as int i=0, it will be repeated with the above definition, and an error will occur when compiling: Duplicate local variable
System.out.println(i);
}
System.out.println("The value of i is now "+i); //This i is a local variable, defined in the for loop i=>100

//Example 6 Find the combination of 1+2+3+...100 
int s=0;
for(int i=1;i<=100;i++){
s=s+i;
}
System.out.println("Complete is"+s);  

//Example 7 Find all numbers between 0-10000 that are divisible by 888, and their number
int count = 0;
for (int i = 0; i < 10000; i++) {
if (i % 888 == 0) {
System.out.println(i);
count++;
}
}
System.out.print("The number that is divisible by 888 is " + count + "");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561049&siteId=291194637