输出不会在 if...else 语句中打印

Output won't print in if...else statement

我是大学计算机科学专业的新生,对所有这些东西都是全新的。我们目前正在使用 java 并且正在做我们的第一个作业,我们必须为基于文本的游戏制作一个战斗计算器。我的问题出在我的 if else 语句中,当我 运行 程序时打印行语句不会打印出来。

    userInput = input.next();

    int action1 = 1;
    action1 = input.nextInt();
    int action2 = 2;
    action2 = input.nextInt();
    int action3 = 3;
    action3 = input.nextInt();
    int action4 = 4;
    action4 = input.nextInt();

    if (userInput == action1){
        System.out.println("You strike the goblin with your sword for 12 damage.");
    }
    else if (userInput.equals(action2)){
        System.out.println("You cast the weaken spell on the goblin.");
    }
    else if (userInput.equals(action3)){
        System.out.println("You focus and charge your magic power.");
    }
    else if (userInput.equals(action4)){
        System.out.println("You run away!");
    }

}

我也不知道如何在这个网站上正确地放置代码,如果有点难以理解,请见谅。但是不管怎样,我在 if...else 语句的输出不打印的地方做错了什么?

嗯,首先,我假设您正在使用 Scanner 来读取用户的输入。如果是这样,input.next() returns a String 因此 if (userInput == action1){ 无效。

此外,(再次假设)如果您更正了这些类型的比较,即使它们具有相同的值,它们也不会匹配。

您的问题在于 equals 方法和 == 运算符之间的区别。

试试这个:

int userInput = input.nextInt();

int action1 = input.nextInt();
int action2 = input.nextInt();
int action3 = input.nextInt();
int action4 = input.nextInt();

if (userInput == action1) {
  System.out.println("You strike the goblin with your sword for 12 damage.");
} else if (userInput == action2) {
  System.out.println("You cast the weaken spell on the goblin.");
} else if (userInput == action3) {
  System.out.println("You focus and charge your magic power.");
} else if (userInput == action4) {
  System.out.println("You run away!");
}

请注意,我稍微更改了 bindings/assignments,userInput 的类型现在是 int

我想您需要类似于下面示例的内容。我不明白你为什么设置 action1 = 1 并使用 action1 = input.nextInt(); 再次阅读它。 action2, action3, action4 的观察结果相同。我还猜想您需要 nextInt() 来表示 userInput,因为您正在将它与 int 进行比较。您还应该考虑用户没有输入正确选择的情况;我在最后添加了一个else

完整示例:

import java.util.Scanner;

public class Test {

    public static void main(String... args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter your choice:"); // if reading from standard input (waiting from the keyboard), this is useful.
        int userInput = input.nextInt();

        int action1 = 1;
        //action1 = input.nextInt(); // I commented this out because I do not think it is useful
        int action2 = 2;
        //action2 = input.nextInt(); // I commented this out because I do not think it is useful
        int action3 = 3;
        //action3 = input.nextInt(); // I commented this out because I do not think it is useful
        int action4 = 4;
        //action4 = input.nextInt(); // I commented this out because I do not think it is useful

        if (userInput == action1) {
            System.out.println("You strike the goblin with your sword for 12 damage.");
        } else if (userInput == action2) {
            System.out.println("You cast the weaken spell on the goblin.");
        } else if (userInput == action3) {
            System.out.println("You focus and charge your magic power.");
        } else if (userInput == action4) {
            System.out.println("You run away!");
        } else{
            System.out.println("Wrong choice!");
        }
    }

}

已编辑:添加输出示例。

编译和运行:

$javac Test.java
==================== run 1
$java Test
Enter your choice:
1
You strike the goblin with your sword for 12 damage.

==================== run 2
$java Test
Enter your choice:
2
You cast the weaken spell on the goblin.

==================== run 3
$java Test
Enter your choice:
3
You focus and charge your magic power.

==================== run 4
$java Test
Enter your choice:
4
You run away!

==================== run 5
$java Test
Enter your choice:
5
Wrong choice!