这个想法是,例如,如果他们选择 * if 会说错误的运算符,请再试一次,但目前如果我选择了错误的运算符,并且如果我选择了正确的运算符,程序需要结束,我似乎无法想办法
我的代码如下
import java.util.Scanner;
public class Uppgift5 {
public static void main (String[] args){
int tal1, tal2;
int sum = 0;
int sub=0;
String operator;
Scanner input = new Scanner (System.in);
Scanner input2 = new Scanner (System.in);
System.out.println("write in first digit");
tal1 = input.nextInt();
System.out.println("Write in 2nd digit ");
tal2 = input.nextInt();
System.out.println("Enter + to add and - subtract ");
operator = input2.nextLine();
while (operator.equals("-") || operator.equals("+")|| operator.equals("*") || operator.equals(("/")) ){
if (operator.equals("+")){
sum = tal1+tal2;
System.out.println("the sum is " + sum);
}
else if (operator.equals("-")){
sub = tal1-tal2;
System.out.println("the subtracted value is " + sub);
}
if (operator.equals("*") || operator.equals("/")){
System.out.println("You have put in the wrong operator, your options are + or -");
}
}
} }