【问题标题】:How do I exit out of this while loop?如何退出这个while循环?
【发布时间】:2014-11-28 03:27:19
【问题描述】:

这是我的代码。我的程序也完成了我需要的一切,但是我将如何退出 while 循环?尝试了不同的选项,但没有任何效果。任何建议或帮助将不胜感激。非常感谢您的宝贵时间。

附: (我想通过用户输入退出循环)所以当他们输入 999 时,它会打印出谢谢并结束。

package project02;
import java.util.Scanner;

public class Project02 
{



public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);

        //Declaring variables for a,b,and c
        double a = 1;
        double b = 8;
        double c = 16;

        //Declaring variables for roots
        double x1 = 0;
        double x2 = 0;

        //Discremenent
        double d = (Math.pow(b, 2) - 4*a*c);

        //Inputs
        System.out.println("Input the values a, b, and c for ax^2+bx+c = 0");
        System.out.println("Input a: ");
        a = scan.nextDouble();

        System.out.println("Input b: ");
        b = scan.nextDouble();

        System.out.println("Input c: ");
        c = scan.nextDouble();

        while (d != 999)
    {   
        if (d > 0)
        {
            x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
            x2 = (-b - Math.sqrt(b*b - 4*a*c))/2*a;

            System.out.println("Root 1 is: " + x1);
            System.out.println("Root 2 is: " + x2);
        }
        else if (d == 0)
        {
            x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;

            System.out.println("There is only one real root at x = " + x1);
        }
        else
        {
            System.out.println("There are no real roots");
        }
        System.out.println("\n" + "Input the values a, b, and c for ax^2+bx+c = 0 or enter 999 to      stop.");

        System.out.println("Input a: ");
        a = scan.nextDouble();

        System.out.println("Input b: ");
        b = scan.nextDouble();

        System.out.println("Input c: ");
        c = scan.nextDouble();

    }   
        System.out.println("Thank you!!!");
    }
}

【问题讨论】:

    标签: java loops while-loop exit


    【解决方案1】:

    在您的情况下,当 a = 999 时,您需要将 break 退出 while 循环。

    像这样:

    System.out.println("Input a: ");
    a = scan.nextDouble();
    
    if(a == 999) break;
    

    至于整个程序,我会改用do while 循环。

    像这样:

    Scanner scan = new Scanner(System.in);
    
    //Variable for continue.
    String response = "n";
    
    //Declaring variables for a,b,and c
    double a = 1;
    double b = 8;
    double c = 16;
    
    //Declaring variables for roots
    double x1 = 0;
    double x2 = 0;
    
    //Discremenent
    double d = (Math.pow(b, 2) - 4*a*c);
    
    
    
    do
    {   
        //Inputs
        System.out.println("Input the values a, b, and c for ax^2+bx+c = 0");
        System.out.println("Input a: ");
        a = scan.nextDouble();
    
        System.out.println("Input b: ");
        b = scan.nextDouble();
    
        System.out.println("Input c: ");
        c = scan.nextDouble();
    
        if (d > 0)
        {
            x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
            x2 = (-b - Math.sqrt(b*b - 4*a*c))/2*a;
    
            System.out.println("Root 1 is: " + x1);
            System.out.println("Root 2 is: " + x2);
        }
        else if (d == 0)
        {
            x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
    
            System.out.println("There is only one real root at x = " + x1);
        }
        else
        {
            System.out.println("There are no real roots");
        }
        System.out.println("Continue?(y/n)");
        response = scan.next();
    
    } while(response.equalsIgnoreCase("y");
    
    System.out.println("Thank you!!!");
    

    希望这会有所帮助!

    【讨论】:

    • 我回去尝试了你的方法,但对于这部分代码: System.out.println("Continue?(y/n)");响应 = scan.nextChar();我收到 scan.nextChar() 的错误;出于某种原因...
    • 我已经编辑了代码。我将 Java 与另一种语言混淆了。 .nextChar() 不存在。而是为您的变量使用字符串。我还将 while() 中的比较更改为更简洁的内容。
    • 终于搞定了。感谢我的男人的所有帮助。不用担心大声笑非常感谢!
    【解决方案2】:

    您需要将 d 设置为 999 才能退出 while 循环。我在你的代码中没有看到。另一种选择是使用'break'。例如:

    System.out.println("Input a: "); 
    a = scan.nextDouble();
    if(a == 999) {
        break;
    }
    

    或者

    System.out.println("Input a: "); 
    a = scan.nextDouble();
    if(a == 999) {
        d = 999;
        continue;
    }
    

    【讨论】:

      【解决方案3】:

      你永远不会设置“d”变量,这意味着它永远不会是 999。 您需要读取用户的输入并在循环内设置 d 变量

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 2015-01-18
        • 2013-08-09
        相关资源
        最近更新 更多