猜0-100之间的数字游戏,有缺点,输入非数字会运行报错
作者:互联网
import java.util.Random;TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back
import java.util.Scanner;
public class demo{
public static void main(String[] args){
Random r =new Random();
int ran =r.nextInt(100)+1; //生成随机数 1-99
Scanner sc =new Scanner(System.in);
System.out.print("欢迎来到猜数字的游戏,请输入您猜的0-100的数字:");
while(true) {
int put = sc.nextInt();
if(put>0&&put<100){
if (put > ran) {
System.out.println("您猜的数过大,请小一点哦");
System.out.println(" ");
System.out.println("请输入您猜想的数字:");
} else if (put < ran) {
System.out.println("您猜的数过小,请大一点哦");
System.out.println(" ");
System.out.println("请输入您猜想的数字:");
}
else {
System.out.println("恭喜您,猜对啦");
break;
}
}
else{
System.out.println("您输入的数字不在0-100之间哦,请重新输入...");
System.out.println(" ");
System.out.println("请输入您猜想的数字:");
}
}
}
}
标签:数字,System,put,报错,println,100,输入您,out 来源: https://www.cnblogs.com/ZYX520/p/16088846.html