Java 如何实现”是否重新开始(y/n)”

来源:百度知道 编辑:UC知道 时间:2024/05/23 18:47:38
现在代码是 在5以内的数任取一个猜,问题是如何实现 重新开始
怎么我的do while没用?
public static void main(String[] args) throws IOException {
// TODO code application logic here
int x;
int a;
boolean again=false;

x=(int)(Math.random()*(5-0)+0);
System.out.println("GOGO,猜猜我是谁!");
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

a: do
{

for(int i=0; ;i++)
{
String z=b.readLine();
a=Integer.parseInt(z);

if(a>x)
{

System.out.println("真可惜.太大了");
continue;

}
else if(a<x)
{ System.out.println("可惜,小了点..");

continue;
}
else if(a==x)
{

do {
。。。。
} while ()

当括号中的表达式 为真 跳出停止循环

again == false 为真 所以只循环了一次 不会出错但没起任何作用

最简单while(true){ }

可以加一点人性化设计 比如 输入 -1 退出 等等

判断语句错了
不是while(again=false);
是again==false

你直接把你的 again==false改成 again!=false