-5
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 if”上。

令牌“else”的语法错误,虽然是预期的

4

4 回答 4

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.");
}
于 2012-04-05T06:59:48.413 回答
2

;从行尾删除if

于 2012-04-05T06:59:57.090 回答
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.");
于 2012-04-05T07:00:54.793 回答
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.");
            }

    }
}
于 2012-04-05T07:05:42.890 回答