/**
* 猜数游戏
*/
public class TestGuessNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random r = new Random();
String answer = null;//声明写在外面
//玩多次
do {
//计算机生成一个10以内的随机数
int cpu = r.nextInt(10);
//如果要猜中再结事,那么下面的代码需要反复执行
do {
//让玩家猜
System.out.println("请输入要猜的数:");
int you = input.nextInt();
//判断
if (you > cpu) {
System.out.println("猜大了。");
} else if (you == cpu) {
System.out.println("猜对了。");
break;
} else {
System.out.println("猜小了。");
}
} while (true);
System.out.println("你还要再猜一次吗yes/no:");
answer = input.next();
} while ("yes".equals(answer));
}
}