我有两个问题,首先是为什么我不能添加运算符,我可以添加第一个和第二个整数但不能添加运算符。
第二个问题是我需要创建一个永无止境的循环,有没有比 while 循环更简单的方法?基本上的想法是,例如,如果他们选择 * 如果会说错误的运算符,请再试一次
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);
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 = input.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);
}
System.out.println("You have put in the wrong operator, your options are + or -");
}
}
}