【发布时间】: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;又读一遍?action2到action4的同样问题? -
我把它设置为字符串用户输入;
标签: java if-statement output