Random r = new Random();
int number = r.nextInt(100) + 1;
while (true)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字");
int x = sc.nextInt();
if ( x > number)
{
System.out.println("猜大了");
}else if ( x
System.out.println("猜小了");
}else{
System.out.println("猜对了");
break;
}
}
先用while循环可以一直猜到对为止,while循环设置一直为真,当猜到时,设置break强制结束
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)