【发布时间】:2018-01-19 03:14:37
【问题描述】:
我今天一直在做一个小项目,在 StackOverflow 和其他地方四处寻找答案后,我遇到了一个障碍。
我一直在尝试用 Java 创建一个选择你自己的冒险风格的游戏,我开始使用三个特征(姓名、种族、性别)创建一个角色。我计划使用这些来允许故事中的分支。
问题是我没有得到我想要的结果。目前,我已经让我的代码工作了,打印语句显示了玩家所做的选择。
//list out character description
System.out.println("You are " + Name + ", a " + characterGender + " " +
characterRace + ".");
我让它正常工作,直到我尝试对给定的输入进行测试以根除无效条目。我尝试使用 do-while 循环和 try-catch 语句,但无论是否有有效输入,它都会向前运行到下一个选项。
我希望实现的总体思路是:
显示输入选项
检查输入以验证其是否有效
如果输入无效,请重新尝试检查其他输入
如果输入有效,则继续下一个问题
//create test to verify validity of race selection
boolean raceBoolean;
if (raceInt < 0 || raceInt > 4) {
raceBoolean = true;
try {
do {
//selection list that defines options
if (raceInt == 1)
characterRace = "human";
else if (raceInt == 2)
characterRace = "elf";
else if (raceInt == 3)
characterRace = "orc";
else if (raceInt == 4)
characterRace = "undead";
}
while (raceBoolean = false);
System.out.println("Nice try. Enter a valid number for one of the four races!");
}
finally {
}
老实说,我很茫然。我知道这很简单,但我正在努力自学。我感谢可能提供的任何帮助。其他主题并不是很有帮助,因为它们并没有提供太多背景来说明为什么会首先出现问题。
提前谢谢你。老实说,我正在努力学习如何正确地做到这一点。
完整代码如下:
import java.util.Scanner;
public class Adventure {
public static void main(String[] args) {
// TODO Auto-generated method stub
//insert input tool
Scanner input = new Scanner(System.in);
//create inventory object for gold and items
int gold = 10;
//create health points object
//address player with greeting
System.out.println("Welcome to your adventure!");
//ask for character name, race, and gender
System.out.println("What will your adventurer's name be?");
String Name = input.nextLine();
System.out.println("What will your adventurer's race be?");
System.out.println("1. Human");
System.out.println("2. Elf");
System.out.println("3. Orc");
System.out.println("4. Undead");
int raceInt = input.nextInt();
String characterRace = "";
//create test to verify validity of race selection
boolean raceBoolean;
if (raceInt < 0 || raceInt > 4) {
raceBoolean = true;
try {
do {
//selection list that defines options
if (raceInt == 1)
characterRace = "human";
else if (raceInt == 2)
characterRace = "elf";
else if (raceInt == 3)
characterRace = "orc";
else if (raceInt == 4)
characterRace = "undead";
}
while (raceBoolean = false);
System.out.println("Nice try. Enter a valid number for one of the four races!");
}
finally {
}
}
//ask for character gender
System.out.println("What will your adventurer's gender be?");
System.out.println("1. Male");
System.out.println("2. Female");
int genderInt = input.nextInt();
String characterGender = "";
if (genderInt == 1)
characterGender = "male";
else if (genderInt == 2)
characterGender = "female";
//list out character description
System.out.println("You are " + Name + ", a " + characterGender + " " + characterRace + ".");
//introduce player to adventure with different introductions for different races
System.out.print("You came to Glassolin with only ten gold pieces to your name. ");
if (characterRace == "human")
System.out.print("Glassolin is home to many humans; some from the far reaches of the realm. You feel right at home, more or less,"
+ " as long as you ignore the occasional odd look from the cityfolk staring at someone dressed in farmer's clothes.");
else if (characterRace == "elf")
System.out.print("While an elf isn't the most common sight around town, the locals seem like they don't care one way or another "
+ "about you wandering the streets. ");
}
}
【问题讨论】:
标签: java loops try-catch do-while