Java 如何退出这个while循环?
这是我的密码。我的程序也做了我需要的一切,但我如何退出while循环??尝试了不同的选择,但没有任何效果。任何建议或帮助都将不胜感激。非常感谢您抽出时间 另外(我想通过用户输入退出循环),所以当他们键入999时,它会打印出来谢谢并结束Java 如何退出这个while循环?,java,loops,while-loop,exit,Java,Loops,While Loop,Exit,这是我的密码。我的程序也做了我需要的一切,但我如何退出while循环??尝试了不同的选择,但没有任何效果。任何建议或帮助都将不胜感激。非常感谢您抽出时间 另外(我想通过用户输入退出循环),所以当他们键入999时,它会打印出来谢谢并结束 package project02; import java.util.Scanner; public class Project02 { public static void main(String[] args) { Sca
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!!!");
}
}
为了退出while循环,需要将d设置为999。我在你的代码中看不到这一点。另一个选项是使用“break”。例如:
System.out.println("Input a: ");
a = scan.nextDouble();
if(a == 999) {
break;
}
或
您从未设置过“d”变量,因此这意味着它从未设置过。
您需要读取用户的输入并在循环内设置d变量在您的情况下,当a=999时,您需要从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)”;response=scan.nextChar();我得到一个scan.nextChar()的错误;出于某种原因…我编辑了代码。我把Java和另一种语言混淆了。nextChar()不存在。而是使用字符串作为变量。我还将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!!!");