我是Java和编码的新手。我已经开始通过these tutorials取得了巨大的成功,直到我到达this one。我知道在我的代码中我还没有进入“楼上”的选项,因为我想确保“厨房”选项首先正常工作。
代码编译得很好,但是当我在cmd行中运行它时,我可以选择进入“厨房”的第一个选项,但是当我选择查看“食品室”时,它需要2厘米线“食品储藏室“执行实际调查。
此外,如果我在查看“食品室”后选择“逃跑”选项,则不会使用“逃跑”选项打印文本。
很抱歉,如果有更简单的方法可以做到这一点,但我还没有学到它们。
感谢您的帮助!
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");}
}
}
答案 0 :(得分:1)
查看本节的逻辑......
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();
如果用户进入“厨房”,则会提示他们refrigerator
或pantry
,如果他们输入pantry
,您会直接进入空提示,实际上并不是处理了用户可能输入refrigerator
您的整个逻辑链都被打破了,您不是将这些部分分成单独的逻辑块来处理当前的情况。
例如,像......
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");
}
}
}
会将每个逻辑块分组到它自己的组中。这将使您能够实际使用方法来进一步隔离逻辑。
你必须面对的下一个问题是当他们没有进入你期望的时候会怎样做
您将面临的另一个问题是Scanner#next
将返回下一项工作,因此run away
之类的内容将无效。相反,您可以考虑使用Scanner#nextLine
代替
答案 1 :(得分:1)
你的逻辑关闭了。看看食品储藏室的实际位置。
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("> ");
}
}