【发布时间】: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