0

我的问题很简单,为什么如果我选择 + m 不加,如果我选择 -

在这里感谢所有帮助。在此先感谢,我在这里做错了什么,我知道我在正确的轨道上

public static void main(String[] args) throws Exception {

    int tal1, tal2;
    char operator;

    Scanner input1 = new Scanner(System.in);
    Scanner input2 = new Scanner(System.in);
    Scanner input3 = new Scanner(System.in);

    System.out.println("Write in a 1st integer");
    tal1 = input1.nextInt();

    System.out.println("Write in 2nd intger");
    tal2 = input2.nextInt();

    System.out.println("Do you want to add or subtract please choose + or -");
    operator = (char) input3.nextInt();

    if (operator == '-') {
        System.out.println("Tal1 - Tal2 = " + (tal1 - tal2));
    } else if (operator == '+') {
        System.out.println("Tal1 + Tal2 = " + (tal1 + tal2));
    }

    System.out.println("Wrong thing to do buddy");

}
4

2 回答 2

0

好朋友:

你有两个错误:

1- 在 public static void main 之前你还没有开始上课。

2-您在此行中有错误。

operator = (char)input3.nextInt();

'+' 和 '-' 不是 int。所以你不能为他们得到一个 int 。

而是使用我为您编写的以下代码:

class Test2
{
public static void main(String[] args) 
{


int tal1, tal2;
char operator;

Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner (System.in);
Scanner input3 = new Scanner (System.in);

    System.out.println("Write in a 1st integer");
    tal1=input1.nextInt();

    System.out.println("Write in 2nd intger");
    tal2=input2.nextInt();




    System.out.println("Do you want to add or subtract please choose + or -");
    operator = input3.nextLine().charAt(0);




    if (operator == '-')
    {
        System.out.println("Tal1 - Tal2 = "+(tal1-tal2)); 
    }

    else if (operator == '+')
    {

            System.out.println("Tal1 + Tal2 = "+(tal1+tal2)); 
    }

        System.out.println("Wrong thing to do buddy");
}


}
于 2013-10-20T20:34:27.637 回答
0

使用next()代替nextInt()和使用字符串代替字符并将字符串与str_1.equals("+")此进行比较,如果字符串匹配,则返回true

于 2013-10-20T20:19:35.200 回答