Home Article Practice 自动猜数代码

自动猜数代码

2022-05-06 15:14  views:477  source:2940164968    

import java.util.Random;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
static public void main(String[] args) {
Random r = new Random();
int rdm = r.nextInt(100) + 1;
int min = 0;
int max = 101;
int num = (min + max) / 2;
while (true) {
System.out.print("Please enter the number you guess:");
int suspect = num;
if (suspect == rdm) {
System.out.println("Congratulations on your guess!");
break;
} else if (suspect < rdm) {
System.out.println("Small");
min = suspect;
} else if (suspect > rdm) {
System.out.println("Big");
max = suspect;
}
System.out.println(suspect);
num = (min + max) / 2;
}
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)