如何退出此循环?

时间:2014-10-03 03:04:39

标签: java loops while-loop exit

这是我的代码。我的程序也做了我需要的一切但是如何退出while循环?试过不同的选择,但没有工作。任何建议或帮助将不胜感激。非常感谢你的时间。

P.S。 (我想通过用户输入退出循环)所以当他们输入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!!!");
    }
}

3 个答案:

答案 0 :(得分:1)

您需要将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;
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在你的情况下,当a = 999时,你需要{while} break

像这样:

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!!!");

希望这有帮助!

相关问题