gpt4 book ai didi

java - 通过简单的 Java "Choose your own adventure"文本游戏工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:59 24 4
gpt4 key购买 nike

我是 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");}

}

}

最佳答案

看看这部分的逻辑...

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();

如果用户进入“厨房”,你会提示他们输入refrigeratorpantry,如果他们输入pantry,你会直接进入一个空提示,您实际上并没有处理用户可能输入除 refrigerator

之外的其他内容的可能性

您的整个逻辑链都被破坏了,您没有将这些部分分成单独的逻辑 block 来处理当前情况。

例如,像...

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");
}
}
}

将每个逻辑 block 分组到它自己的组中。然后,这将使您能够实际使用方法来进一步隔离逻辑。

下一个你必须克服的问题是当他们没有输入你期望的内容时该怎么办

您将面临的另一个问题是Scanner#next 将返回下一个工作,因此类似run away 的操作将不起作用。相反,您可能会考虑使用 Scanner#nextLine

关于java - 通过简单的 Java "Choose your own adventure"文本游戏工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31236190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com