【问题标题】:How to catch a value and tell the user to input again?如何捕获一个值并告诉用户再次输入?
【发布时间】:2020-02-15 20:56:14
【问题描述】:

我正在尝试创建一个游戏角色,并且要求用户输入一个介于 0-18 之间的数字。我输入 19 以查看代码是否会告诉我再试一次,但它只是忽略它然后打印出字符。它也对魔法数量做同样的事情。用户应该输入 0 和 50 之间,但如果我输入一个大于 50 的数字,它仍然会打印它。我希望代码告诉我输入错误,然后重试。有没有办法让用户在不使用异常的情况下重试?

public class Character {

    private String name;
    private int strength;

    public Character() {
        name = "TBD";
        strength = 15;
    }

    public Character(String name, int strength) {
        this.name = name;
        this.strength = strength;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;   
    }

    public int getStrength() {
        return strength;
    }

    public void setStrength(int strength) {
        int count = 1;
        while (true) { 
            if (strength >= 0 && strength <= 18) {
                System.out.println("Value is out of range 0-18");
                System.out.println("Please try again");
                break;
            }
            else {
                this.strength = strength;
                count = 0;
            }
        }
    }

    public String toString() {
        return "Name: " + name + ", Strength: " + strength;
    }
}
public class Human extends Character {

    private String name;
    private int strength;
    private String weapon;
    private int magicAmount;

    public Human(String name, int strength, String weapon, int magicAmount) {
        this.name = name;
        this.strength = strength;
        this.weapon = weapon;
        this.magicAmount = magicAmount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStrength() {
        return strength;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public String getWeapon() {
        return weapon;
    }

    public void setWeapon(String weapon) {
        this.weapon = weapon;
    }

    public int getMagicAmount() {
        return magicAmount;
    }

    public void setMagicAmount(int magicAmount) {
        int count = 1;
        while (count != 0) {
            if ((magicAmount >= 0 && magicAmount <= 50)) {
                System.out.println("Value is out of range 0-50");
                System.out.println("Please try again");
                break;
            }
            else {
                this.magicAmount = magicAmount;
                count = 0;
            }           
        }
    }

    public String toString() {
        return "Human [Name: " + name + ", Strength: " + strength + ", Weapon: " 
                + weapon + ", Magic Amount: " + magicAmount + "]";
    }
}

    ArrayList<Character> characters = new ArrayList<Character>();

    String keepLooping = "y";

    do {
        System.out.println("Which character would you like to create (Human, Robot, or Animal): ");
        String choice = input.nextLine();

        if (choice.equalsIgnoreCase("Human")) {

            System.out.println("Enter a name for the Human: ");
            String name = input.next();

            System.out.println("Enter the amount of stength for Human (0-18): ");
            int strength = input.nextInt();

            input.nextLine();
            System.out.println("Enter a weapon for Human (Sword or Dagger): ");
            String weapon = input.nextLine();

            System.out.println("Enter the magic amount for your selected weapon (0-50): ");
            int magicAmount = input.nextInt();

            Human charH = new Human(name, strength, weapon, magicAmount);
            characters.add(charH);
        }

        input.nextLine();
        System.out.println("Would you like to create more characters - y or n");
        keepLooping = input.nextLine();
    }

    while (keepLooping.equalsIgnoreCase("y"));

    for (int i = 0; i < characters.size(); i++) {
        Character c = characters.get(i);
        System.out.println(c);
    }
}

【问题讨论】:

    标签: java if-statement input java.util.scanner do-while


    【解决方案1】:

    您应该对用户输入进行验证,例如(强度):

    Integer strength = null;
    while (strength == null) {
        System.out.println("Enter the amount of stength for Human (0-18): ");
        strength = input.nextInt();
    
        if (strength < 0 || strength > 18) {
            strength = null;
            System.out.println("Invalid strength, please try again");
        }
    }
    

    另一个问题是您在setStrength 中进行了验证登录,但从未调用过,因为您在构造函数中设置了强度。所以解决方法是:

    public Character(String name, int strength) {
        setName(name);
        setStrength(strength);
    }
    

    【讨论】:

    • 我可以再问一个问题吗?我试图用字符串 weapon 做同样的事情,但是当我输入列出的武器时,它告诉我我错了,它一直在循环。
    • 你能在这里评论一下武器验证的代码吗?
    • ` 字符串武器 = ""; while (weapon == "") { System.out.println("输入人类武器(剑或匕首):");武器 = input.nextLine();如果(武器!=“剑”||武器!=“匕首”){武器=“”; System.out.println("武器无效,请重试"); } }`
    • 对于字符串使用 .equals() 而不是 ==
    • 不管对错还是一直循环。
    【解决方案2】:

    关于创建人类的代码存在几个问题。

    问题 1

    第一个问题是您使用全参数构造函数创建 Human。这样就永远不会调用 setStrength(...) 和 setMagicAmount(...)。

    解决方案 1

    利用设置器。请注意,这仍然不起作用。见问题 2 和 3。

    public Human(String name, int strength, String weapon, int magicAmount) {
        this.name = name;
        //this.strength = strength;
        setStrength(strength); // not this will still not work! Problem 2 and 3
        this.weapon = weapon;
        //this.magicAmount = magicAmount;
        setMagicAmount(magicAmount); // Will still not work. See Problem 3
    }
    

    解决方案 2

    您可以使用无参数构造函数,然后调用设置器,而不是使用全参数构造函数。还是不行。 Se 问题 2 和 3。

    Human human = new Human();
    human.setName(name);
    human.setStrength(strength); // will not work. See problem 2 and 3
    human.setWeapon(weapon);
    human.setMagicAmount(magicAmount); // will not work. See problem 3
    

    问题 2

    类 Human 扩展了 Character,但您仍然覆盖了人类内部的 setStrength(...) 方法。此方法没有任何规则。

    在 Human 内部删除此方法,将调用具有规则的 Character 中正确的 setStrength(...),因为它是继承的。这仍然行不通。见问题 4。

    问题 3

    您在 setStrength(...) 和 setMagicAmount(...) 中有范围检查规则。问题是这两种方法的范围检查都是错误的,您不需要循环。

    解决方案

    首先检查值。如果超出范围告诉用户,则设置该值。

    对于 setStrength(...) 更改为

    public void setStrength(int strength) {
        if (strength < 0 || strength > 18) { // less than 0 or more than 18
            throw new IllegalArgumentException("Value is out of range 0-18");
        }
        this.strength = strength;
    } 
    

    对于 setMagicAmount(...) 更改为

    public void setMagicAmount(int magicAmount) {
        if (magicAmount < 0 || magicAmount > 50) { // less than 0 or more than 50
            throw new IllegalArgumentException("Value is out of range 0-50");
        }
        this.magicAmount = magicAmount;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2021-10-23
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      相关资源
      最近更新 更多