package pack;
import java.util.Scanner;
public class Calculator {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
String cont = "Yes";
while(cont == "Yes" || cont == "yes"){
System.out.print("Enter a Number: ");
int x = scan.nextInt();
System.out.print("Enter another Number: ");
int y = scan.nextInt();
int diff = x - y;
int sum = x + y;
int prod = x * y;
int quot = x/y;
System.out.println("The Sum is: " + sum);
System.out.println("The Diffrence is: " + diff);
System.out.println("The Product is: " + prod);
System.out.println("The quotient is: " + quot);
System.out.print("Enter Yes to Continue: ");
cont = scan.next();
System.out.println(cont);
}
}
}
此整個代碼工作,但while循環不重複。 cont = scan.next();
捕捉字符串。輸出如下:Java雖然循環將不會循環
[
Enter a Number: 5
Enter another Number: 6
The Sum is: 11
The Diffrence is: -1
The Product is: 30
The quotient is: 0
Enter Yes to Continue: Yes
Yes
]
然後程序終止沒有任何問題。我需要它來讓while循環重複。謝謝您的幫助!
當你比較字符串時,你必須使用equals()。 – Sajmon
工作過的很棒!謝謝您的幫助! – Ixen