1

我正在上我的第一个 Java 课程,并且很难完成这项作业。

任务本身非常简单。我所要做的就是提示用户输入一个整数、一个字符,然后是另一个整数。

如果输入的字符是+,两个整数应该相加。如果输入的字符是-,则两个整数相减。ETC...

问题是我不知道如何从用户那里接受一个字符,然后在我的 If/Switch 语句中使用它。

这是我的提示:

// Prompt the user to enter an integer
    System.out.print("Please enter an integer number:");
    int int1  = input.nextInt();

// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
    System.out.print("Enter 0 for +, 1 for -, 2 for /, 3 for %, and 4 for *");
    int char1 = input.nextInt();

// Prompt the user to enter another integer
    System.out.print("Please enter another integer number:");
    int int2 = input.nextInt();

这是我的 If/Switch 语句:(我知道它们是多余的,但它是作业的一部分)

if (char1 == 0) {
    int ans = int1 + int2;
    System.out.println(int1 + " + " + int2 + " = " + ans);
} else if (char1 == 1) {
    int ans = int1 - int2;
    System.out.println(int1 + " - " + int2 + " = " + ans);
} else if (char1 == 2) {
    int ans = int1 / int2;
    System.out.println(int1 + " / " + int2 + " = " + ans);
} else if (char1 == 3) {
    int ans = int1 % int2;
    System.out.println(int1 + " % " + int2 + " = " + ans);
} else if (char1 == 4) {
    int ans = int1 * int2;
    System.out.println(int1 + " * " + int2 + " = " + ans);
} else
    System.out.println("Invalid operator");

int result = 0;
switch (char1) {
    case 0: result = int1 + int2;
            System.out.println(int1 + " + " + int2 + " = " + result); break;
    case 1: result = int1 - int2;
            System.out.println(int1 + " - " + int2 + " = " + result); break;
    case 2: result = int1 / int2;
            System.out.println(int1 + " / " + int2 + " = " + result); break;
    case 3: result = int1 % int2;
            System.out.println(int1 + " % " + int2 + " = " + result); break;
    case 4: result = int1 * int2;
            System.out.println(int1 + " * " + int2 + " = " + result); break;
    default: System.out.println("Invalid operator");
}

其他一切都很好。我只是想从使用 0 表示 +、1 表示 - 更改为仅使用 +、-、/、% 和 *。

4

3 回答 3

2

您可以将用户的运算符作为StringChar

像这样 :

 Scanner input = new Scanner(System.in);
        // Prompt the user to enter an integer
        System.out.print("Please enter an integer number:");
        int int1 = input.nextInt();

// Prompt the user to enter a character
// Since I could not use characters, I am using integers to represent them.
        System.out.print("Enter +, -,  /,  %, *");
        char char1 = input.next().charAt(0);

// Prompt the user to enter another integer
        System.out.print("Please enter another integer number:");
        int int2 = input.nextInt();

        if (char1=='+') {
            int ans = int1 + int2;
            System.out.println(int1 + " + " + int2 + " = " + ans);
        } else if (char1=='-') {
            int ans = int1 - int2;
            System.out.println(int1 + " - " + int2 + " = " + ans);
        } else if (char1=='/') {
            int ans = int1 / int2;
            System.out.println(int1 + " / " + int2 + " = " + ans);
        } else if (char1=='%') {
            int ans = int1 % int2;
            System.out.println(int1 + " % " + int2 + " = " + ans);
        } else if (char1=='*') {
            int ans = int1 * int2;
            System.out.println(int1 + " * " + int2 + " = " + ans);
        } else {
            System.out.println("Invalid operator");
        }

        int result = 0;

        switch (char1) {
            case '+':
                result = int1 + int2;
                System.out.println(int1 + " + " + int2 + " = " + result);
                break;
            case '-':
                result = int1 - int2;
                System.out.println(int1 + " - " + int2 + " = " + result);
                break;
            case '/':
                result = int1 / int2;
                System.out.println(int1 + " / " + int2 + " = " + result);
                break;
            case '%':
                result = int1 % int2;
                System.out.println(int1 + " % " + int2 + " = " + result);
                break;
            case '*':
                result = int1 * int2;
                System.out.println(int1 + " * " + int2 + " = " + result);
                break;
            default:
                System.out.println("Invalid operator");
        }
于 2013-02-03T19:28:08.000 回答
1

你可以这样做:

System.out.print("Enter +, -, /, or *");
char char1 = input.nextLine().charAt(0);

nextLine将获取用户输入的全部内容,并charAt获取指定索引处的字符。

因此,charAt(0)获取第一个字符。这是一个直观的解释:顶部是字符串,底部是它的字符编号:

|t|h|i|s| |i|s| |a| |t|e|s|t|
-----------------------------
|0|1|2|3|4|5|6|7|8|9|1|1|1|1|
| | | | | | | | | | |0|1|2|3|
于 2013-02-03T19:20:10.483 回答
0

您可以通过这种方式使用char来自用户输入的 a:

Achar只是一个无符号整数值 0..65535 所以你可以写例如+43[Which is the unicode of '+']

switch (char1) {
    case '+': result = int1 + int2;
        System.out.println(int1 + " + " + int2 + " = " + result); break;
    case '-': result = int1 - int2;
        System.out.println(int1 + " - " + int2 + " = " + result); break;
    case '/': result = int1 / int2;
        System.out.println(int1 + " / " + int2 + " = " + result); break;
    case '%': result = int1 % int2;
        System.out.println(int1 + " % " + int2 + " = " + result); break;
    case '*': result = int1 * int2;
        System.out.println(int1 + " * " + int2 + " = " + result); break;
    default: System.out.println("Invalid operator");
}
于 2013-02-03T19:21:00.057 回答