Java猜数字,猜完一局以后,输入y继续下一次游戏,否则结束
作者:互联网
代码如下:
public static void main(String[] args) {
String x = "";
do {
int random = (int) (Math.random()*100);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要猜的数字:");
int num = scanner.nextInt();
if(num == random){
System.out.println("恭喜你,猜对了!");
}else{
System.out.println("抱歉,你猜错了!");
}
System.out.println("请输入y继续?否则结束!");
x = scanner.next();
}while ("y".equals(x));
System.out.println("游戏结束!");
}
运行结果:
标签:scanner,一局,Java,游戏,int,random,System,println,out 来源: https://www.cnblogs.com/hg-blogs/p/15449558.html