我的程序包括求解方程式并询问用户是否要求更多方程,所以即使我输入yes继续但循环终止,这是我的代码
public static void main(String[] args) {
String goAgain;
double A, B, C, solution;
Scanner scanner = new Scanner(System.in);
System.out.println("This program will print a solution of an equation");
System.out.println("of the form A*X*X + B*X + C = 0, where A, B, and");
System.out.println("C are values that you enter.");
do {
System.out.println();
System.out.println("Enter values of A, B and C : ");
System.out.print(" A = ");
A = scanner.nextDouble();
System.out.print(" B = ");
B = scanner.nextDouble();
System.out.print(" C = ");
C = scanner.nextDouble();
System.out.println();
try {
solution = root(A, B, C);
System.out.println("your solution is : " + solution);
} catch (IllegalArgumentException e) {
System.out.println("Sorry, I can't find a solution.");
System.out.println(e.getMessage());
}
System.out.println("Do you want to solve another equation ? ");
goAgain = scanner.next();
System.out.println(goAgain);
} while (goAgain == "yes");
}
答案 0 :(得分:0)
在.equals()
中使用==
Fucntion而不是while (goAgain == "yes");
。它应该是while (goAgain.equals("yes");
请注意,goAgain == "yes"
始终返回false
。