2012-04-05 89 views
-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."); 

      { 

    } 
} 
} 

當我寫了這個代碼,我得到下面所述的錯誤,我不知道什麼是錯! 這是我的錯誤,他們apear對「否則,如果」在底部。我「否則,如果」命令不能正常工作。

令牌 「其他」

語法錯誤,而預計

+0

縮進你的代碼格式正確,它會使其更具可讀性。它是一個好主意,始終使用conditionnal語句和循環,優質的保險事括號。 – Snicolas 2012-04-05 07:01:39

+0

我個人有點像省略括號在某些情況下,因爲我覺得他們只是多餘的視覺上的混亂(在某些情況下)。但是,官方的[Java編碼約定](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#449)要求在括號總是用於if語句。因此,即使我有時會覺得省略大括號,我總是會支持我的ifs。 – Alderath 2012-04-05 07:14:14

+0

作爲旁註,詮釋命中= randGen.nextInt(10)+ 1 + charlightcc;如果你使用10的charlightcc總是會導致命中,不知道這是否意圖行爲 – Peter 2012-04-05 07:40:52

回答

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."); 
} 
+0

哦!我明白!我不明白你的意思是我要刪除所有的!謝謝! – Emil 2012-04-05 07:03:55

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."); 
0

這就是爲什麼它幾乎總是即使您不嚴格需要,也可以更好地使用{}。如果添加{}很明顯這是怎麼回事..

如果我們重新格式化您的代碼利爾:

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."); 
      } 

    } 
}