The source code for guessing numbers is as follows:
import java.util.Scanner;
import java.util.Random;
public class Lan{ public static void main(String args[]) { Scanner reader=new Scanner( System.in ); Random random=new Random (); System.out.println("Give you an integer between 1 and 100, please guess this number"); int realNumber =random.nextInt(100)+1; int yourGuess =0; System.out.println ("Enter your guess:"); yourGuess = reader.nextInt(); while(yourGuess!=realNumber) { if(yourGuess>realNumber) { System.out.println("I guess it’s big, input your guess: ”); yourGuess=reader.nextInt(); } else if(yourGuess<realNumber) { System.out.println("It's small, and then enter your guess:");
yourGuess=reader.nextInt();
}
}
System.out.println("猜对了");
}
}
Principle: