Java的do while循环在条件为假时不会停止。

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

Java do while loop doesn‘t stop when condition is false

问题

循环即使在 while 中的布尔值为 false 时仍然继续进行。我不确定问题出在哪里。布尔循环变量应该在答案未被缓存时设置为 false,从而退出循环,但出于某些原因,程序未能执行此操作。

    public static double getvalue(String unit) {
        double answer = 0;
        boolean loop = true;
        do {
            try {
                System.out.println("请输入" + unit);
                answer = sc.nextDouble();
                loop = false;
            } catch (Exception e) {
                System.out.println("必须输入一个 double 值");
                sc.nextLine();
            }
        } while (loop);
        return answer;
    }

*编辑:在第11次查看代码后,以及在完成更多代码后,结果发现问题实际上出现在另一个循环中,我没有正确解决它,导致它一直不断地调用这个循环。我已经修复了这个问题,现在它正常工作。非常感谢你,对此我感到很抱歉。

英文:

The loop still goes on even when the boolean in the while is false. I'm not sure what's wrong. The boolean loop variable is supposed to be set to false if they answer isn't cached releasing you from the loop but for some reason, the program isn't doing that.

    public static double getvalue(String unit) {
        double answer = 0;
        boolean loop = true;
        do {
            try {
                System.out.println("enter the " + unit);
                answer = sc.nextDouble();
                loop = false;
            } catch (Exception e) {
                System.out.println("has to be a double");
                sc.nextLine();
            }
        } while (loop);
        return answer;
    }

*Edit: After looking through my code for the 11th time and after finishing more of it, it turns out the problem was in a different loop at the top which I didn't resolve correctly making it to keep calling this loop over and over. I fixed it and now it works. Thanks you so much and sorry about that

答案1

得分: 3

以下是翻译好的代码部分:

public static double getValue(String unit) {
    Scanner keys = new Scanner(System.in);
    double answer = 0;

    System.out.println("输入一个双精度数:");
    while (true) {
        try {
            answer = keys.nextDouble();
            return answer;
        } catch (Exception exc) {
            System.out.println("数字必须是双精度数!");
            keys.nextLine();
        }
    }
}
英文:

This is what I would do--

public static double getValue(String unit) {
    Scanner keys = new Scanner(System.in);
    double answer = 0;

    System.out.println("Enter a double: ");
    while (true) {
        try {
            answer = keys.nextDouble();
            return answer;
        } catch (Exception exc) {
            System.out.println("number has to be a double!");
            keys.nextLine();
        }
    }
  }

答案2

得分: 0

尝试这个方法:

public static double getvalue(String unit) {
    double answer = 0;
    boolean loop = true;
    do {  
        try {
            System.out.println("输入" + unit);
            answer = (double) sc.nextDouble();
            break;
        } catch (Exception e) {
            System.out.println("必须是一个双精度数");
            sc.nextLine(); 
        }
    } while (true);
    return answer;
}
英文:

Try this way:

public static double getvalue(String unit) {
double answer = 0;
boolean loop = true;
do {  
try {
         System.out.println("enter the " + unit);
         answer = (double) sc.nextDouble();
         break;
    } catch (Exception e) {
        System.out.println("has to be a double");
        sc.nextLine(); 
        }
   } while (true);
   return answer;
}

huangapple
  • 本文由 发表于 2020年4月9日 04:04:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109115.html
匿名

发表评论

匿名网友

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

确定