【发布时间】: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