Java Getting Started Day 6

Case 1: Jump control statement

public class day06{ public static void main(String[] args){ for(int i =1;i<=5;i++){ if(i%2==0){ //continue;----If yes ture terminates the cycle and enters this time



			//break----直接结束循环
			
		}
		System.out.println(i);
	}
	
	
}
}

Insert picture description here
Case 2: Loop nesting

The loop statement contains the loop statement.
Requirement: output the hour and minute of a day on the console
. The range of minute and hour:
minute: 0<=minute<60
hour: 0<=hour<24

public class ForForDemo{ public static void main (String[] args){ /* System.out.println("0 hours 0 minutes"); System.out.println("0 hours 0 minutes"); System.out.println ("0 hours 0 minutes"); System.out.println("0 hours 0 minutes"); System.out.println("---------------"); System. out.println("0 hour 0 minute"); System.out.println("0 hour 0 minute"); System.out.println("0 hour 0 minute"); System.out.println("0 hour 0 Min"); System.out.println("0-----------"); System.out.println("0 hour 0 minute"); System.out.println("0 hour 0 Min"); System.out.println("0 hours 0 minutes"); System.out.println("0 hours 0 minutes"); }















}



//循环改进:


for(int minute=0;minute<4;minute++){
	
	System.out.println("0时"+minute+"分");
}
	

System.out.println("------------------------------------------------------------");
for(int minute=0;minute<4;minute++){
	
	System.out.println("1时"+minute+"分");
}

System.out.println("------------------------------------------------------------");


for(int minute=0;minute<4;minute++){
	
	System.out.println("2时"+minute+"分");
}
System.out.println("------------------------------------------------------------");
for(int minute=0;minute<4;minute++){
	
	System.out.println("3时"+minute+"分");
	
	
	
	
}
}
}

for(int hour = 0;hour<24;hour++){ //First enter the initial statement of the outer loop, and then enter the initial statement of the inner loop until the end of the inner loop, and then return to the outer loop.
for(int minute=0;minute<60;minute++){ //The outer loop controls the range of hours, and the inner loop controls the range of minutes
System.out.println(hour + "Hour" + minute + "分");
}
System. out.println("---------------------------------");

}

}

}

Insert picture description here
Insert picture description here

Insert picture description here
Case 3: Obtain 10 random numbers by loop

import java.util.Random;
public class RandomDemo{

public static void main (String[] args){
	Random r =new Random();
	
	int number =r .nextInt(10);
	System.out.println("number:" + number);
	
	
}

}

import java.util.Random;
public class RandomDemo{

public static void main (String[] args){
	Random r =new Random();                                       // 用循环获取10个随机数
	for(int i = 0;i<10 ;i++){
		
		int number =r .nextInt(10);
	System.out.println("number:" + number);
	}
	
	
	
	
}

}

import java.util.Random;
public class RandomDemo{

public static void main (String[] args){
	Random r =new Random();                                       // 用循环获取10个随机数
	for(int i =0;i<101;i++){
		
		int number =r .nextInt(101);
	System.out.println("number:" + number);
	}
	
	
	
	
}

}

Insert picture description here

Insert picture description here

Insert picture description here
Random function and use steps:

Role: used to generate a random number

Steps for usage:

1: Guide package:

inport java.util.Random; //The action of guiding the package must appear above the class definition

2: Create an object: Random r =new Random(0; //In the above format, r is a variable name, it can be changed, and others are not allowed to change

3: Get random numbers

Int number= r.nextInt(10); //The obtained number includes 0 but not 10 number is the variable name can change the number 10 can change. No other changes are allowed

//

Guess you like

Origin blog.csdn.net/weixin_51599540/article/details/109347799