import java.util.Random;
public class knife {
static Random randGen = new Random();
public static void main(String[] args) {
int kniferpd = 1;
int knifebullet = 0;
int charstrength = 10;
int charlightcc = 10;
int hit = randGen.nextInt(10) + 1 + charlightcc;
int crit = randGen.nextInt(100) + 1 + charlightcc;
int knifedmg = randGen.nextInt(5) + 6;
if (hit < 10)
System.out.println("You missed!");
else if (hit > 10 && crit > 98);
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
else if (hit > 10 && crit < 98);
System.out.println("Strike of " + knifedmg + "dmg.");
{
}
}
}
当我写这段代码时,我得到下面所述的错误,我不知道什么是错的! 这是我的错误,他们在底部的“其他如果”中表示。
令牌“else”上的语法错误,而预期
答案 0 :(得分:2)
删除if语句后的;
这样做
if (hit < 10)
System.out.println("You missed!");
else if (hit > 10 && crit > 98)
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
else if (hit > 10 && crit < 98)
System.out.println("Strike of " + knifedmg + "dmg.");
或
if (hit < 10) {
System.out.println("You missed!");
}
else if (hit > 10 && crit > 98) {
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
}
else if (hit > 10 && crit < 98) {
System.out.println("Strike of " + knifedmg + "dmg.");
}
答案 1 :(得分:2)
使用;
if
答案 2 :(得分:1)
只是一个小错误......
if (hit < 10)
System.out.println("You missed!");
else if (hit > 10 && crit > 98); // <-- No ; here
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
else if (hit > 10 && crit < 98); // <-- No ; here
System.out.println("Strike of " + knifedmg + "dmg.");
为了您的理解,您所写的内容将被解释为:
if (hit < 10) {
System.out.println("You missed!");
} else if (hit > 10 && crit > 98) {
; // same as do nothing
}
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
else if (hit > 10 && crit < 98){ // compiler error
; // same as do nothing
}
System.out.println("Strike of " + knifedmg + "dmg.");
答案 3 :(得分:0)
这就是为什么即使你不严格需要if语句也要使用{}几乎总是更好的原因。如果你添加{}它显然正在发生什么......
如果我们将您的代码重新格式化为:
import java.util.Random;
public class knife {
static Random randGen = new Random();
public static void main(String[] args) {
int kniferpd = 1;
int knifebullet = 0;
int charstrength = 10;
int charlightcc = 10;
int hit = randGen.nextInt(10) + 1 + charlightcc;
int crit = randGen.nextInt(100) + 1 + charlightcc;
int knifedmg = randGen.nextInt(5) + 6;
if (hit < 10) {
System.out.println("You missed!");
} else if (hit > 10 && crit > 98);
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
else if (hit > 10 && crit < 98); // this doesn't make sense
System.out.println("Strike of " + knifedmg + "dmg.");
{
}
}
}
正确格式化并带有更正
import java.util.Random;
public class knife {
static Random randGen = new Random();
public static void main(String[] args) {
int kniferpd = 1;
int knifebullet = 0;
int charstrength = 10;
int charlightcc = 10;
int hit = randGen.nextInt(10) + 1 + charlightcc;
int crit = randGen.nextInt(100) + 1 + charlightcc;
int knifedmg = randGen.nextInt(5) + 6;
if (hit < 10) {
System.out.println("You missed!");
} else if (hit > 10 && crit > 98) {
System.out.println("Critical strike of " + knifedmg * 2 + "dmg.");
} else if (hit > 10 && crit < 98) {
System.out.println("Strike of " + knifedmg + "dmg.");
}
}
}