我是Java和编码的新手。我已经开始通过these tutorials取得了巨大的成功,直到获得this one为止。我知道在我的代码中,我还没有进入“上楼”的选项,因为我想确保“kitchen”选项先正确运行。

代码可以正常编译,但是当我在cmd行中运行它时,我可以选择进入“厨房”的第一个选项,但是当我选择查看“pantry”时,它需要2条cmd行和“pantry”来执行实际调查。

另外,如果在查看“储藏室”后选择“运行”选项,则不会使用“运行”选项打印文本。

抱歉,如果有更简单的方法可以执行此操作,但是我还没有学到它们。

谢谢你的帮助!

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的可能性

您的整个逻辑链已断开,您没有将这些部分分成多个单独的逻辑块来处理当前场景。

例如,类似...
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

08-05 09:59