真的是新来的java和这个任务有一些麻烦。该任务是:简单的Java计算器 - while循环直到答案为0
编写打印欢迎 消息的简单的计算器程序,接受来自用户的简单的算术表达式,并且执行 所请求的操作。你的程序应该重复这个 ,直到两个操作数为0,然后退出。
它运行良好,但我不知道如何得到While循环的句柄,以便计算器将继续,直到答案为0.对不起,如果这是一个非常基本的问题。任何帮助,将不胜感激。
import java.util.Scanner;
class Calculator{
public static void main(String[] args)
{
System.out.println("Welcome to CSC 210 Calculator by Bob the Builder!");
System.out.println("Enter an integer operation: ");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
String operation= input.next();
int y = input.nextInt();
while(x + y != 0){
if(operation.equals("+")){
System.out.println(x + y);
}
else if(operation.equals("-")){
System.out.println(x - y);
}
else if(operation.equals("*")){
System.out.println(x * y);
}
else if(operation.equals("/")){
System.out.println(x/y);
}
else if(operation.equals("%")){
System.out.println(x % y + y);
}
else {
System.out.println("Operation is invalid.");
}
System.out.println("Enter an integer operation: ");
if(x + y != 0);
break;
}
}
}
你可以做的是=假'并运行你的代码,直到'flag'为false。一旦操作为'= 0',将您的标志设置为true。 – smr5 2014-09-25 00:42:13