【问题标题】:Check out this code看看这段代码
【发布时间】:2017-10-17 11:26:12
【问题描述】:

我需要帮助来查找我的代码中的错误并修复它。 这是代码:

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)

感谢您的帮助:)

【问题讨论】:

  • 欢迎来到 Stack Overflow!看来您需要学习使用调试器。请自行掌握一些补充调试技术。
  • @IvanPronin 我想你忘记了补充调试技术......
  • 你试过debug your program吗?

标签: java execution


【解决方案1】:

您得到一个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.");
    }
}

【讨论】:

    【解决方案2】:

    当您调用s.nextInt(); 时,scanner 对象仅读取该行的整数部分,但不考虑回车。因此,当您调用下一个s.nextLine() 时,它不会读取下一行,实际上是读取之前读取的整数的提示(回车)。 因此,要清理线路的“垃圾”,只需在每个 nextInt(); 后添加一个 s.nextLine();

    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

    【讨论】:

      猜你喜欢
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2017-10-13
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多