小编Amr*_*ngh的帖子

当用户输入错误的值时,代码会继续进入无限循环

我添加了一个while循环,以便在用户输入无效值时,代码将重新提示用户输入有效值.但是当用户输入无效值时,代码进入无限循环.任何帮助将非常感激 !!

public static void main(String[] args) {
    System.out.println("Usage : enter a, b, & c from a quadratic equation");
    System.out.println("        aX^2 +bX + c = 0");
    System.out.println("        Result will be 2 values for X");

    Scanner in = new Scanner(System.in);
    double a = 0;
    double b = 0;
    double c = 0;
    double x1 = 0 ;
    double x2 = 0;
    double discriminant = 0;

    System.out.println("Please enter values for a , b, and c ");

    while(true){
        try
        {
            a = in.nextDouble(); …
Run Code Online (Sandbox Code Playgroud)

java loops while-loop

2
推荐指数
1
解决办法
74
查看次数

标签 统计

java ×1

loops ×1

while-loop ×1