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