java 猜数字
所属分类 java
浏览量 860
java 猜数字游戏
javac Guess.java
java Guess
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class Guess {
public static void main(String[] args) throws Exception {
int magic, guess, counter = 0;
String reply;
Random rand = new Random();
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do {
counter = 0;
magic = rand.nextInt(100) + 1;
do {
System.out.print("Please guess a magic number:");
guess = readInt(br);
while (guess <= 0 || guess > 100) {
System.out.print("please input a int number 1~100 :");
guess = readInt(br);
}
counter++;
if (guess > magic)
System.out.print("Wrong!Too big!\n");
else if (guess < magic)
System.out.print("Wrong!Too small!\n");
else
System.out.print("Right!\n");
} while (guess != magic && counter < 10);
System.out.print("counter = " + counter + "\n");
System.out.print("The magic is " + magic + "\n");
System.out.print("Do you want to continue(Y/N or y/n)?");
reply = br.readLine();
} while (reply != null && reply.equalsIgnoreCase("Y"));
}
private static int readInt(BufferedReader br) {
try {
String str = br.readLine();
return Integer.parseInt(str);
} catch (Throwable e) {
return 0;
}
}
}
C语言猜数字
上一篇
下一篇
全球ETF行业发展2020年度报告
c语言整数进制
C语言猜数字
go猜数字游戏
Rust安装
python猜数字小游戏