英文:
Exiting from while loop not working in java
问题
我是Java编程的新手。我想要计算总和,并且如果用户输入"N",我想要退出程序;如果用户输入"Y",我想要再次循环。但是,即使我输入"N",它也不能使我退出循环。
public class Program {
public static void main(String[] args) {
boolean a = true;
while (a) {
System.out.println("输入一个数字");
Scanner c = new Scanner(System.in);
int d = c.nextInt();
System.out.println("输入另一个数字");
Scanner ce = new Scanner(System.in);
int df = ce.nextInt();
int kk = d + df;
System.out.println("总和为:" + kk);
System.out.println("是否继续(y/n)?");
Scanner zz = new Scanner(System.in);
boolean kkw = zz.hasNext();
if (kkw) {
a = true;
} else {
a = false;
System.exit(0);
}
}
}
}
我不知道我在哪里出错了?还有其他的方法吗?
英文:
I am new to java programming.I want to calculate the sum and want to exit the program if user enters "N" and again loop if user enters "Y".But,it is not getting me out of loop even I enter "N".
public class Program {
public static void main(String[] args) {
boolean a=true;
while (a) {
System.out.println("enter a number");
Scanner c=new Scanner(System.in);
int d=c.nextInt();
System.out.println("enter a number2");
Scanner ce=new Scanner(System.in);
int df=ce.nextInt();
int kk=d+df;
System.out.println("total sum is"+kk);
System.out.println("do you want to continue(y/n)?");
Scanner zz=new Scanner(System.in);
boolean kkw=zz.hasNext();
if(kkw) {
a=true;
}
else {
a=false;
System.exit(0);
}
}
}
I didnt know where I made the mistake? Is there any other way?
答案1
得分: 4
首先,你的a
变量在scanner.hasNext()
为真时也为真,导致每次输入时a
都是true
,包括输入为"N"
的情况。这意味着你的while
循环将持续进行,直到没有更多的输入。
其次,你可以优化你的代码如下:
- 我建议去掉
a
和kkw
,以使你的代码更清晰、更简洁。 - 只使用一个
Scanner
,并且将其定义在循环外部。你不需要为同一输入使用多个Scanner
。而且,每次循环都初始化一个Scanner
会消耗资源。 - 使用有意义的变量名。编程不仅应该高效,还应该易于阅读。在这个小代码中,这只是一个小问题,但想象一下如果是整个程序,你不得不为每个变量的含义进行搜索,而不是添加功能和修复错误。
以下是你的代码的优化且可工作的版本:
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("输入一个数字");
int input1 = scanner.nextInt();
scanner.nextLine(); // nextInt()不会移动到下一行
System.out.println("输入第二个数字:");
int input2 = scanner.nextInt();
scanner.nextLine();
System.out.println("总和为:" + (input1 + input2)); /* 重要的是要用括号括起来,以便告诉编译器input1 + input2是一个计算,而不是将"总和为"附加上去*/
System.out.println("是否继续? (Y/N)");
if (scanner.hasNext() && scanner.nextLine().equalsIgnoreCase("n"))
break;
}
scanner.close();
英文:
First of all, your a
variable is true if scanner.hasNext()
is true, leading to a
being true
with every input, including "N"
which means, your while
loop will keep on going until there are no more inputs.
Second of all, you could optimize your code the next way:
- I suggest getting rid of
a
andkkw
to make your code cleaner and shorter. - Use only one
Scanner
and define it outside of the loop. You don't need more than oneScanner
for the same input. Also, initializing aScanner
with every loop is resource-consuming. - Use meaningful variable names. Programming should not only be efficient, but also easy to read. In this tiny code it's a minor issue but imagine having an entire program and, instead of adding features and bug-fixing, you had to search for the meaning of every variable.
Here's an optimized and working version of your code:
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a number");
int input1 = scanner.nextInt();
scanner.nextLine(); // nextInt() doesn't move to the next line
System.out.println("Enter a second number:");
int input2 = scanner.nextInt();
scanner.nextLine();
System.out.println("Total sum is " + (input1 + input2)); /* Important to
surround the sum with brackets in order to tell the compiler that
input1 + input2 is a calculation and not an appending of
"Total sum is "*/
System.out.println("Do you want to continue? (Y/N)");
if (scanner.hasNext() && scanner.nextLine().equalsIgnoreCase("n"))
break;
}
scanner.close();
答案2
得分: 0
try (Scanner in = new Scanner(System.in)) {
boolean done = false;
while (!done) {
System.out.println("输入第一个数字");
int d = in.nextInt();
System.out.println("输入第二个数字");
int df = in.nextInt();
int kk = d + df;
System.out.println(String.format("总和为 %d", kk));
System.out.println("是否继续(y/n)?");
String cont = in.next();
done = cont.equalsIgnoreCase("n");
}
} catch(Exception e) {
e.printStackTrace();
}
英文:
try (Scanner in = new Scanner(System.in)) {
boolean done = false;
while (!done) {
System.out.println("enter first number");
int d = in.nextInt();
System.out.println("enter second number");
int df = in.nextInt();
int kk = d + df;
System.out.println(String.format("total sum is %d", kk));
System.out.println("do you want to continue(y/n)?");
String cont = in.next();
done = cont.equalsIgnoreCase("n");
}
} catch(Exception e) {
e.printStackTrace();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论