Java可以选择添加还是删除

Java可以选择添加还是删除,java,if-statement,Java,If Statement,我有两个问题,第一个是为什么我不能添加运算符,我可以添加第一个和第二个整数,但不能添加运算符 第二个问题是我需要创建一个永无止境的循环有没有比while循环更简单的方法?基本上,他们的想法是,例如,如果他们选择了*如果他们会说错误的运算符,请再试一次 import java.util.Scanner; public class Uppgift5 { public static void main (String[] args){ int tal1, tal2; int su

我有两个问题,第一个是为什么我不能添加运算符,我可以添加第一个和第二个整数,但不能添加运算符

第二个问题是我需要创建一个永无止境的循环有没有比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 -");
}

}

}

当您按下键盘上的回车键时,
nextInt
方法不会使用您输入的空白字符(换行符)。你什么时候打电话来

operator = input.nextLine();
所有读到的都是新行字符。你需要再加一个

nextLine();
nextInt()

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();
input.nextLine();
System.out.println("Enter + to add and - subtract ");
operator = input.nextLine();

要创建一个循环,该循环将一直运行,直到提供有效的输入,并且与您的
while
构造不同,请尝试以下方法:

String input;
do {
    input = getInput(); //Replace this with however you get your input
} while(!(input.equals("+") || input.equals("-")));
但是,如果希望通知用户,则需要使用类似于当前的
while
循环。Java中的循环类型不多,而
while
循环也很简单。通知用户的一段代码可以如下所示:

String input;
while(true) {
    input = getInput();
    if(input.equals("+") || input.equals("-")) break; //Exit loop if valid input
    System.out.println("Invalid input");
}

//Do arithmetic here

至于你的第一个问题,我不完全清楚你所说的“添加操作员”是什么意思。你能澄清一下吗?

这是我在你另一篇文章中的回答

import java.util.Scanner;


public class tt {
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 ");

while (true){

operator = input2.nextLine();
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 -");
    break;
  }
  }
 }
}