【问题标题】:Output won't print in if...else statement [duplicate]输出不会在 if...else 语句中打印 [重复]
【发布时间】:2015-09-10 17:44:47
【问题描述】:

我是大学计算机科学专业的大一新生,对所有这些东西我完全陌生。我们目前正在使用 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 语句的输出不会打印的地方做错了什么?

【问题讨论】:

  • 为什么不放整个代码?
  • 你的变量userInput是什么类型的?
  • 我最初是这样做的,但这是我第一次使用这个网站,整个代码的格式对我来说不正确。
  • 为什么设置action1 = 1;又读一遍? action2action4 的同样问题?
  • 我把它设置为字符串用户输入;

标签: java if-statement output


【解决方案1】:

首先,我假设您使用Scanner 来读取用户的输入。如果是这样,input.next() 返回 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!");
}

请注意,我稍微更改了绑定/分配,userInput 的类型现在是 int

【讨论】:

  • 我改变了一切,就像你在这里一样,它仍然无法打印。 'String option1 = " 1.) Sword Attack"; System.out.println(option1); String option2 = " 2.) 施法"; System.out.println(option2); String option3 = " 3.) 充能法力"; System.out.println(option3); String option4 = " 4.) 逃跑"; System.out.println(option4);'
  • 我仍然不知道如何在这个网站上格式化代码,抱歉。但我在最后一条评论中给出的是我在程序运行时可供玩家选择的选项。我仍然对如何设置一个数字来打印出动作感到困惑
  • @kplandry 你的问题没有很好地说明,所以答案是猜测,我只是在下面添加了一个,它可能对你有用。
  • @kplandry 您没有在原始源代码中发布实际“选项”的书面选项;没有人能弄清楚你问的是action11.) Sword Attack
【解决方案2】:

我猜你需要类似下面的例子。我不明白你为什么设置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!

【讨论】:

  • 当我运行程序时,第一行(输入您的选择:)被打印出来,但是当我输入一个数字时,要打印的行(你打妖精...... , 你施放了弱化咒语......等)不打印。希望这能让我的问题更清楚一点
  • @kplandry 只是为了确保:您必须在输入号码后按 键。然后,我提供的这个示例应该打印您的一条消息或我添加的最后一条消息“错误选择”。
  • 当我输入前 4 个动作之一时,没有输出,但是当我输入 1、2、3 或 4 以外的数字时,“错误选择!”消息打印。这会导致什么吗?
  • 这很奇怪,正如您可以看到答案的编辑,我实际上运行了代码并获得了每个选择的预期结果。您可以在这里复制完整的示例并尝试一下吗?我的意思是把你的原创作品放在一边。
  • 我发现了我的问题。我真的不知道如何解释它,但是您发布的这个示例确实帮助了我,并且输出现在正在打印所有操作。非常感谢!