java猜数字游戏怎么怎么做到在游戏结束后输入y重新开始输入n退出游戏?下面是源代码

import java.util.Scanner;

public class youxi1 {

/**
* @param args
*/
public static void main(String[] args) {
int b =(int)(100*Math.random());
Scanner scan = new Scanner(System.in);
System.out.println("请输入1个100以内的数字:");
int a = scan.nextInt();int i=1;
while(a!=b&&i<20)
{System.out.println(a>b?"大了":"小了");
a = scan.nextInt();
i++;
}
String s ="";
if(i<5){s="你猜对了,真厉害";
}else if(i<10){
s="你猜对了,成绩不错";
}else if(i<20){
s="你猜对了,成绩一般";
}else s="20次都猜不对,你真笨";
System.out.println("结果是:"+b+"你猜的次数是:"+i+","+s);
System.out.println("谢谢您的使用");
}

}

while(true){
//你的程序
System.out.println("再玩一次?");
String st= scan.next();
if( st=="y"){continue;}

if( st=="n"){break;}
System.out.println("既不是y也不是n,出错");
}追问

为什么不管输入y还是n都会重新运行

追答

对了在String st= scan.next();前加一句scan = new Scanner(System.in);

不然你看看大小写吧 你把st print出来试试

温馨提示:答案为网友推荐,仅供参考
相似回答