看看这段代码

时间:2017-05-17 20:35:44

标签: java execution

我需要帮助才能在我的代码中找到错误并修复它。 这是代码:

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)

感谢您的帮助:)

2 个答案:

答案 0 :(得分: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.");
    }
}

答案 1 :(得分:0)

当您调用s.nextInt();时,扫描仪对象只读取该行的整数部分,但它不会考虑回车。因此,当您调用下一行s.nextLine()时,它不会读取下一行,实际上是读取之前读取的整数的提醒(回车)。 所以要清理&#34;垃圾&#34;该行,只需在每个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)

由于与继承相关的事情,我可以在你的课程中看到(Boxer,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