1. To guess the random number in this chapter, you first need to use the Scanner statement to guess how many sets of wins, draws and losses the user needs. If you want to use a few sets, you must use the for loop, and to achieve randomness, you need to use the Math method , for random sampling.
1. Scanner
first uses the Scanner statement to capture user data to confirm that the user needs several sets of random wins, draws and losses.
2.
The principle of the for loop cycle:
it is to do the same thing repeatedly.
For example: climbing stairs 1-->
running laps on the playground on the 5th floor 1-->5 laps
1--5 laps
start from where 1 lap starts
and ends 5 The conditions for judging the end point of the circle
change when running
If you want to execute a normal cycle in the program, it is similar to real life. It needs to meet three conditions (necessary):
initial value, end point judgment condition, change amount
It is allowed to write all three necessary conditions in ()
for (1 initial value; 258 end point judgment condition; 47 variation) { 36 a lot of code to be executed; } use the loop to get several groups of numbers to realize the output of multiple random values # ### 3. Math uses the Math method to define random numbers and achieve the results we want through calculation logic to achieve randomness. According to the above code write:
import java.util.Scanner;//First import the package public class SJB {//Create a Demo type file public static void main(String[] args){//write an environment Scanner input=new Scanner(System.in);//new a Scanner to grab user data System.out.println("Please enter the wins and losses of the crew you want to get");//prompt the user int Temp=input.nextInt();//grab the data output by the user and store it in the int type Temp for (int j=0;j<Temp;j++) { double value = Math.random(); int number = (int) (value * 9 + 1); if(number>4&number<6){ System.out.println("win"); }else if(number>2&number<4){ System.out.println("平"); }else if(number>0&number<2){ System.out.println("Negative"); } System.out.println(" "); } } }
Output demo: