do-while循环中的java语义问题
我的程序由解方程和询问用户是否想解更多方程组成,因此,即使我输入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");
}
# 1 楼答案
在
while (goAgain == "yes");
中使用.equals()
函数而不是==
应该是
while (goAgain.equals("yes");
记住
goAgain == "yes"
总是返回false