After completing the number guessing game, the user enters a number, determines whether the number is greater than, less than, or equal to a randomly generated number, and exits the program when it is equal.
//随机数生成
import java.util.Random;
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
//随机数生成
Random random=new Random();
Scanner scanner=new Scanner(System.in);
//随机数取1-100之间
int cguess= random.nextInt(100)+1;
while(true){
System.out.println("请输入您猜的数:");
int pguess=scanner.nextInt();
//猜的值>本次随机生成值
if(pguess>cguess){
System.out.println("您的值偏高");
}//猜的值<本次随机生成值
else if(pguess<cguess){
System.out.println("您的值偏低");
}猜的值=本次随机生成值
else{
System.out.println("恭喜你,猜中了!");
break;
}
}
}
}
Achieving the result: