[英]Can't make String equal method and Converting Strings to Numbers work together.
It is a Java beginner question. 这是一个Java初学者的问题。 The program is not complicated, it just asks the user input grade and then finish it when the user input "y" , and then calculate max, min,average, and total.
该程序并不复杂,它只是询问用户输入的等级,然后在用户输入“ y”时完成输入,然后计算最大值,最小值,平均值和总计。
My trouble is that I can't stop the loop when I type y. 我的麻烦是输入y时无法停止循环。 Thank you so much, if anyone can help me out.
非常感谢,如果有人可以帮助我。 I have tried many approaches to figure that.
我尝试了很多方法来解决这个问题。
do
{
System.out.println("please go on");
input = keyboard.next();
grade = Integer.parseInt(input);
InputTimes++;
if (grade>Maxgrade)
{
Maxgrade = grade;
}
if(Mingrade>grade)
{
Mingrade= grade;
}
if (input.equals("y"))
{
GameOver = true;
System.out.println("yyyyyyy");
break;
}
else
{
GameOver = false;
System.out.println("nnnnnn");
}
totalgradeNoF = grade+totalgradeNoF;
}while(GameOver==false);
Scanner keyboard = new Scanner(System.in);
int Maxgrade = 0, Mingrade = 0, InputTimes = 0, grade = 0, totalgradeNoF = 0;
boolean GameOver = false;
String input = "";
do {
System.out.println("Please go on : ('y' to stop)");
input = keyboard.nextLine();
if (input.equals("y")) {
GameOver = true;
} else {
grade = Integer.parseInt(input);
InputTimes++;
if (grade > Maxgrade) {
Maxgrade = grade;
}
if (Mingrade > grade) {
Mingrade = grade;
}
totalgradeNoF = grade + totalgradeNoF;
}
} while (!GameOver);
Some things to change : 有些事情要改变:
break
so break
这样 !GameoOver
instead of GameOver==false
!GameoOver
代替GameOver==false
Gameover=false
at each iteration, just at the initialisation Gameover=false
设置为初始化 当您输入“ y”时,NumberFormatException即将到来,因此您需要处理此异常,然后在键入“ y”后可以退出循环。
Use for Scanner methods nextInt()
and nextLine()
for reading input. 用于Scanner方法
nextInt()
和nextLine()
来读取输入。
EX:- 例如:-
Scanner scan = new Scanner(System.in);
then we can use scan.nextInt()
for get int 然后我们可以使用
scan.nextInt()
来获取int
and we can use scan.nextLine()
for get Strings 我们可以使用
scan.nextLine()
获取字符串
https://www.tutorialspoint.com/java/util/scanner_next.htm https://www.tutorialspoint.com/java/util/scanner_next.htm
this link may be help for you. 此链接可能对您有帮助。
sorry for my english. 对不起我的英语不好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.