退出 while 循环在 Java 中无法正常工作。

huangapple go评论137阅读模式
英文:

Exiting from while loop not working in java

问题

  1. 我是Java编程的新手我想要计算总和并且如果用户输入"N"我想要退出程序如果用户输入"Y"我想要再次循环但是即使我输入"N"它也不能使我退出循环
  2. public class Program {
  3. public static void main(String[] args) {
  4. boolean a = true;
  5. while (a) {
  6. System.out.println("输入一个数字");
  7. Scanner c = new Scanner(System.in);
  8. int d = c.nextInt();
  9. System.out.println("输入另一个数字");
  10. Scanner ce = new Scanner(System.in);
  11. int df = ce.nextInt();
  12. int kk = d + df;
  13. System.out.println("总和为:" + kk);
  14. System.out.println("是否继续(y/n)?");
  15. Scanner zz = new Scanner(System.in);
  16. boolean kkw = zz.hasNext();
  17. if (kkw) {
  18. a = true;
  19. } else {
  20. a = false;
  21. System.exit(0);
  22. }
  23. }
  24. }
  25. }
  26. 我不知道我在哪里出错了还有其他的方法吗
英文:

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".

  1. public class Program {
  2. public static void main(String[] args) {
  3. boolean a=true;
  4. while (a) {
  5. System.out.println("enter a number");
  6. Scanner c=new Scanner(System.in);
  7. int d=c.nextInt();
  8. System.out.println("enter a number2");
  9. Scanner ce=new Scanner(System.in);
  10. int df=ce.nextInt();
  11. int kk=d+df;
  12. System.out.println("total sum is"+kk);
  13. System.out.println("do you want to continue(y/n)?");
  14. Scanner zz=new Scanner(System.in);
  15. boolean kkw=zz.hasNext();
  16. if(kkw) {
  17. a=true;
  18. }
  19. else {
  20. a=false;
  21. System.exit(0);
  22. }
  23. }
  24. }

I didnt know where I made the mistake? Is there any other way?

答案1

得分: 4

首先,你的a变量在scanner.hasNext()为真时也为真,导致每次输入时a都是true,包括输入为"N"的情况。这意味着你的while循环将持续进行,直到没有更多的输入。

其次,你可以优化你的代码如下:

  1. 我建议去掉akkw,以使你的代码更清晰、更简洁。
  2. 只使用一个Scanner,并且将其定义在循环外部。你不需要为同一输入使用多个Scanner。而且,每次循环都初始化一个Scanner会消耗资源。
  3. 使用有意义的变量名。编程不仅应该高效,还应该易于阅读。在这个小代码中,这只是一个小问题,但想象一下如果是整个程序,你不得不为每个变量的含义进行搜索,而不是添加功能和修复错误。

以下是你的代码的优化且可工作的版本:

  1. Scanner scanner = new Scanner(System.in);
  2. while (true) {
  3. System.out.println("输入一个数字");
  4. int input1 = scanner.nextInt();
  5. scanner.nextLine(); // nextInt()不会移动到下一行
  6. System.out.println("输入第二个数字:");
  7. int input2 = scanner.nextInt();
  8. scanner.nextLine();
  9. System.out.println("总和为:" + (input1 + input2)); /* 重要的是要用括号括起来,以便告诉编译器input1 + input2是一个计算,而不是将"总和为"附加上去*/
  10. System.out.println("是否继续? (Y/N)");
  11. if (scanner.hasNext() && scanner.nextLine().equalsIgnoreCase("n"))
  12. break;
  13. }
  14. 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:

  1. I suggest getting rid of a and kkw to make your code cleaner and shorter.
  2. Use only one Scanner and define it outside of the loop. You don't need more than one Scanner for the same input. Also, initializing a Scanner with every loop is resource-consuming.
  3. 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:

  1. Scanner scanner = new Scanner(System.in);
  2. while (true) {
  3. System.out.println("Enter a number");
  4. int input1 = scanner.nextInt();
  5. scanner.nextLine(); // nextInt() doesn't move to the next line
  6. System.out.println("Enter a second number:");
  7. int input2 = scanner.nextInt();
  8. scanner.nextLine();
  9. System.out.println("Total sum is " + (input1 + input2)); /* Important to
  10. surround the sum with brackets in order to tell the compiler that
  11. input1 + input2 is a calculation and not an appending of
  12. "Total sum is "*/
  13. System.out.println("Do you want to continue? (Y/N)");
  14. if (scanner.hasNext() && scanner.nextLine().equalsIgnoreCase("n"))
  15. break;
  16. }
  17. scanner.close();

答案2

得分: 0

  1. try (Scanner in = new Scanner(System.in)) {
  2. boolean done = false;
  3. while (!done) {
  4. System.out.println("输入第一个数字");
  5. int d = in.nextInt();
  6. System.out.println("输入第二个数字");
  7. int df = in.nextInt();
  8. int kk = d + df;
  9. System.out.println(String.format("总和为 %d", kk));
  10. System.out.println("是否继续(y/n)?");
  11. String cont = in.next();
  12. done = cont.equalsIgnoreCase("n");
  13. }
  14. } catch(Exception e) {
  15. e.printStackTrace();
  16. }
英文:
  1. try (Scanner in = new Scanner(System.in)) {
  2. boolean done = false;
  3. while (!done) {
  4. System.out.println("enter first number");
  5. int d = in.nextInt();
  6. System.out.println("enter second number");
  7. int df = in.nextInt();
  8. int kk = d + df;
  9. System.out.println(String.format("total sum is %d", kk));
  10. System.out.println("do you want to continue(y/n)?");
  11. String cont = in.next();
  12. done = cont.equalsIgnoreCase("n");
  13. }
  14. } catch(Exception e) {
  15. e.printStackTrace();
  16. }

huangapple
  • 本文由 发表于 2020年3月16日 23:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/60709118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定