通过简单的 Java“选择你自己的冒险”文本游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31236190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working thru simple Java "Choose your own adventure" text game
提问by dzur19
I am new to Java and coding in general. I have started working thru these tutorialswith great success, until I got to this one. I know in my code I have not entered the option of going "upstairs" yet, because I wanted to make sure the "kitchen" option worked correctly first.
我是 Java 和一般编码的新手。我已经开始学习这些教程并取得了巨大的成功,直到我找到了这个教程。我知道在我的代码中我还没有输入“上楼”的选项,因为我想首先确保“厨房”选项正常工作。
The code compiles fine, however when I run it in a cmd line, I am able to choose the first option of going into the "kitchen" but when I choose to view the "pantry" it takes 2 cmd lines with "pantry" to execute actually looking into it.
代码编译得很好,但是当我在 cmd 行中运行它时,我可以选择进入“厨房”的第一个选项,但是当我选择查看“储藏室”时,它需要 2 行带有“储藏室”的 cmd 行执行实际调查它。
Also, if I choose the option to "run away" after looking in the "pantry", it does not println the text with the "run away" option.
另外,如果我在查看“储藏室”后选择“逃跑”选项,它不会用“逃跑”选项打印文本。
Sorry if there are easier ways to do this, however I have not learned them yet.
抱歉,如果有更简单的方法可以做到这一点,但我还没有学会。
Thanks for any help!
谢谢你的帮助!
import java.util.Scanner;
public class Adventure1
{
public static void main( String[] args ){
Scanner keyboard = new Scanner(System.in);
String Go, Look, Pantry, Eat;
System.out.println( " WELCOME TO MY TINY ADVENTURE");
System.out.println(" ");
System.out.println( " You are in a creepy house! Would you like to go 'upstairs' or into the 'kitchen'? ");
System.out.print( "> ");
Go = keyboard.next();
if (Go.equalsIgnoreCase("kitchen"))
{System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");}
System.out.print("> ");
Look = keyboard.next();
if (Look.equalsIgnoreCase( "refrigerator" ))
{System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");}
System.out.print("> ");
Eat = keyboard.next();
if (Eat.equalsIgnoreCase("Yes"))
{System.out.println(" ");
System.out.println("You live!");}
else if (Eat.equalsIgnoreCase("No"))
{System.out.println(" ");
System.out.println("You die of starvation!");}
else if (Look.equalsIgnoreCase( "pantry" ))
{System.out.println("There is a killer inside. Do you want to 'fight' them, or 'run away'?");}
System.out.print("> ");
Pantry = keyboard.next();
if (Pantry.equalsIgnoreCase("fight"))
{System.out.println(" ");
System.out.println("You're weak and die");}
else if(Pantry.equalsIgnoreCase("run away"))
{System.out.println(" ");
System.out.println("You died because your too slow & can't run");}
}
}
采纳答案by MadProgrammer
Have a look at the logic for this section...
看看这个部分的逻辑......
if (Go.equalsIgnoreCase("kitchen")) {
System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");
}
System.out.print("> ");
Look = keyboard.next();
if (Look.equalsIgnoreCase("refrigerator")) {
System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");
}
System.out.print("> ");
Eat = keyboard.next();
If the user enters the "kitchen", you prompt them for refrigerator
or pantry
, if they enter pantry
, you go straight to a empty prompt, you've not actually handled the eventuality that the user might enter something else other then refrigerator
如果用户进入“厨房”,你会提示他们输入,refrigerator
或者pantry
,如果他们输入pantry
,你会直接进入一个空提示,你实际上没有处理用户可能输入其他内容的可能性refrigerator
Your entire logic chain is broken, you're not separating the sections into individual blocks of logic to handle the current scenario.
你的整个逻辑链都被破坏了,你没有将这些部分分成单独的逻辑块来处理当前的场景。
For example, something like...
例如,像...
Scanner keyboard = new Scanner(System.in);
String Go, Look, Pantry, Eat;
System.out.println(" WELCOME TO MY TINY ADVENTURE");
System.out.println(" ");
System.out.println(" You are in a creepy house! Would you like to go 'upstairs' or into the 'kitchen'? ");
System.out.print("> ");
Go = keyboard.next();
if (Go.equalsIgnoreCase("kitchen")) {
System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");
System.out.print("> ");
Look = keyboard.next();
if (Look.equalsIgnoreCase("refrigerator")) {
System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");
System.out.print("> ");
Eat = keyboard.next();
if (Eat.equalsIgnoreCase("Yes")) {
System.out.println(" ");
System.out.println("You live!");
} else if (Eat.equalsIgnoreCase("No")) {
System.out.println(" ");
System.out.println("You die of starvation!");
}
} else if (Look.equalsIgnoreCase("pantry")) {
System.out.println("There is a killer inside. Do you want to 'fight' them, or 'run away'?");
System.out.print("> ");
Pantry = keyboard.next();
if (Pantry.equalsIgnoreCase("fight")) {
System.out.println(" ");
System.out.println("You're weak and die");
} else if (Pantry.equalsIgnoreCase("run away")) {
System.out.println(" ");
System.out.println("You died because your too slow & can't run");
}
}
}
would group each logic block into it's own group. This would then lead you to the ability to actually use methods to further isolate the logic.
会将每个逻辑块分组到它自己的组中。这将使您能够实际使用方法来进一步隔离逻辑。
The next problem you will have to over come is what do to when they don't enter what you're expecting
您必须解决的下一个问题是当他们没有输入您期望的内容时该怎么办
Another problem you will face is Scanner#next
will return the next work, so something like run away
won't work. Instead you might consider using Scanner#nextLine
instead
您将面临的另一个问题是Scanner#next
将返回下一个工作,因此类似的事情run away
将不起作用。相反,你可以考虑使用Scanner#nextLine
替代
回答by Kai Arakawa
Your logic was off. Look at where the pantry actually was.
你的逻辑不对。看看食品储藏室实际上在哪里。
import java.util.Scanner;
public class Adventure1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String Go, Look, Pantry, Eat;
System.out.println(" WELCOME TO MY TINY ADVENTURE");
System.out.println(" ");
System.out
.println(" You are in a creepy house! Would you like to go 'upstairs' or into the 'kitchen'? ");
System.out.print("> ");
Go = keyboard.next();
if (Go.equalsIgnoreCase("kitchen")) {
System.out
.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");
}
System.out.print("> ");
Look = keyboard.next();
if (Look.equalsIgnoreCase("refrigerator")) {
System.out
.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");
} else if (Look.equalsIgnoreCase("pantry")) {
System.out
.println("There is a killer inside. Do you want to 'fight' them, or 'run away'?");
Pantry = keyboard.next();
if (Pantry.equalsIgnoreCase("fight")) {
System.out.println(" ");
System.out.println("You're weak and die");
}
else if (Pantry.equalsIgnoreCase("run away")) {
System.out.println(" ");
System.out
.println("You died because your too slow & can't run");
}
}
System.out.print("> ");
Eat = keyboard.next();
if (Eat.equalsIgnoreCase("Yes")) {
System.out.println(" ");
System.out.println("You live!");
}
else if (Eat.equalsIgnoreCase("No")) {
System.out.println(" ");
System.out.println("You die of starvation!");
}
System.out.print("> ");
}
}