2017-05-17 133 views
-8

我需要幫助以在代碼中查找錯誤並修復它。 下面是代碼:查看此代碼

import java.util.*; 

public class Main 
{ 
     public static void main(String[] args) { 
      Random r = new Random(); 
      Scanner s = new Scanner(System.in); 
      Fighter f1 = new Fighter(); 
      Fighter f2 = new Fighter(); 
      System.out.println("Enter the name of the first fighter:"); 
      String str=s.nextLine(); 
      f1.name=str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase(); 
      System.out.println("Choose the style:\n1/ Boxing\n2/ Kick Boxing\n3/ MMA"); 
      int style1 = s.nextInt(); 
      switch (style1){ 
       case 1 :f1 =new Boxer(); 
        break; 
       case 2 :f1 = new Kickeboxer(); 
        break; 
       case 3 :f1 = new MMA_Fighter(); 
       break;} 
      System.out.println("Enter the name of the seconde fighter:"); 
      String str1=s.nextLine(); 
      f2.name=str1; 
      System.out.println("Choose the style:\n1/ Boxing\n2/ Kick Boxing\n3/ MMA"); 
      int style2 = s.nextInt(); 
      switch (style2){ 
       case 1 :f2 = new Boxer(); 
        break; 
       case 2 :f2 = new Kickeboxer(); 
        break; 
       case 3 :f2 = new MMA_Fighter(); 
       break;} 
      for (int rnd=1;rnd <=5;rnd++){ 
       f1.punch=r.nextInt(f1.power); 
       f2.punch=r.nextInt(f2.power); 
       f1.hp-=f2.punch; 
       f2.hp-=f1.punch; 
       if (f1.hp <= 0){ 
        System.out.println("Round "+rnd+" result:\n K.O !"); 
        break; 
       }else if (f2.hp <= 0){ 
        System.out.println("Round "+rnd+" result:\n K.O !"); 
        break; 
       }else{ 
        System.out.println("Round "+rnd+" result:\n"+f1.name+"'s health : "+f1.getHp()+"\n"+f2.name+"'s health : "+f2.getHp()+"\n **********");} 
      if (f1.hp>f2.hp){ 
       System.out.println(f1.name+" wins !"); 
      }else if(f1.hp<f2.hp){ 
       System.out.println(f2.name+" wins !"); 
      }else {System.out.println("It's a DRAW !");} 
     }//end for 
    }} 
class Fighter { 
     String name; 
     int hp; 
     int power; 
     int punch; 
     public int getHp() { 
      return hp; 
     } 
} 
class Boxer extends Fighter{ 
    int hp = 100; 
    int power = 20; 
} 
class Kickeboxer extends Fighter{ 
    int hp =80; 
    int power = 40; 
} 
class MMA_Fighter extends Fighter{ 
    int hp = 70; 
    int power = 50; 
} 

當執行它,它給了我一個錯誤

Enter the name of the first fighter: 
Choose the style: 1/ Boxing 2/ Kick Boxing 3/ MMA 
Enter the name of the seconde fighter: 
Choose the style: 1/ Boxing 2/ Kick Boxing 3/ MMA 

    Exception in thread "main" java.util.InputMismatchException  at 
java.util.Scanner.throwFor(Unknown Source)  at 
java.util.Scanner.next(Unknown Source)  at 
java.util.Scanner.nextInt(Unknown Source) at 
java.util.Scanner.nextInt(Unknown Source) at Main.main(Main.java:26) 

感謝您的幫助:)

+1

歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助您自己完成一些互補的調試技術。 –

+2

@IvanPronin我想你忘了互補調試技術... – shmosel

+0

你試過[調試你的程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) ? –

回答

0

你得到一個InputMismatchException因爲你打電話s.nextInt(),但輸入無法解析爲int。除了數字之外,用戶可能鍵入了其他字符。通過捕獲此異常,可以使您的界面更加便於用戶使用。

boolean choseStyle = false; 
while (!choseStyle) { 
    try { 
     System.out.println("Choose the style:\n1/ Boxing\n2/ Kick Boxing\n3/ MMA"); 
     int style2 = s.nextInt(); 
     choseStyle = true; 
    } catch (InputMismatchException e) { 
     System.out.println("Please enter a number between 1 and 3."); 
    } 
} 
0

當你調用s.nextInt();掃描對象只讀取行的整數部分,但它並沒有考慮回車。所以當你調用下一個s.nextLine()它不會讀下一行,實際上是讀取之前讀取的整數提示(回車)。 所以清理該行的「垃圾」,只需要加一個s.nextLine();每個nextInt();

int style2 = s.nextInt(); 
s.nextLine(); 

後,您將有一個IllegalArgumentException

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive 
    at java.util.Random.nextInt(Unknown Source) 
    at com.stackoverflow.Main.main(Main.java:49) 

因爲繼承我能看到相關的東西你類(義和團,Kickboxer和MMA_Fighter)。

要解決它,我推薦給你這樣做:

class Fighter { 
    String name; 
    int hp; 
    int power; 
    int punch; 

    public int getHp() { 
     return hp; 
    } 

    public int getPower() { 
     return power; 
    } 

    public void setPower(int power) { 
     this.power = power; 
    } 

} 

class Boxer extends Fighter { 
    int hp = 100; 
    int power = 20; 

    public int getHp() { 
     return hp; 
    } 

    public int getPower() { 
     return power; 
    } 
} 

class Kickeboxer extends Fighter { 
    int hp = 80; 
    int power = 40; 

    public int getHp() { 
     return hp; 
    } 

    public int getPower() { 
     return power; 
    } 
} 

class MMA_Fighter extends Fighter { 
    int hp = 70; 
    int power = 50; 

    public int getHp() { 
     return hp; 
    } 

    public int getPower() { 
     return power; 
    } 
} 

還有另一種方式來解決這個問題,但現在我認爲它會工作。 瞭解更多繼承檢查此鏈接:https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html