-4

这是我第一次来这里,我对编程很陌生,我已经学习 Java 2 周了,我只知道一些基础知识,所以我决定通过做一个简单的文本冒险游戏来测试我的知识,而不使用我没有的对象还没有掌握。我将继续打印故事并描述情况,并接受玩家的选择继续下去。但是我的代码有问题,如果用户输入了无效的选择,我不知道如何重复问题,我尝试了几种方法,但程序要么结束,要么给我无限循环,我需要一些帮助,请.

这是我的代码:

import java.util.Scanner;

class The_Crime {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome Stranger, Please Enter Your Name");
        String name = input.nextLine();
        System.out.println("Welcome" + name);       
        Street street = new Street();

        System.out.println("You are now in the street with a dead body on the floor drowned in blood \n there is a building right ahead of you\n What would you like to do?");
        String choice = input.nextLine();
        while(true) {
            if(choice.equals("enter building")) {
                System.out.println("You have entered the empty building");     
            } else {
                System.out.println("You are now in the street with a dead body lying around drawned in blood \n there is a building right infront of you\n What would you like to do?");
            }
        }    
    }   
}
4

5 回答 5

1

你的 while 状态将永远持续下去,因为你永远不会break摆脱它。但是在这种情况下你不需要做只是改变

while(true)
     if (choice.equals("enter building"))
          System.out.println("You have entered the empty building");

     else 
          System.out.println("You are now in the street with a dead body lying around drawned in blood \n there is a building right infront of you\n What would you like to do?");

while(choice.equals("some invalid choice"))
{
    choice = input.nextLine();
}

或者

while(!choice.equals("some valid choice"))
{
    choice = input.nextLine();
}
于 2013-07-31T05:08:56.307 回答
1

在这种情况下,我建议使用 do-while 循环,第一次你必须问的问题,后来的决定是基于选择的

Scanner input = new Scanner(System.in);
String choice=null;

do{

 System.out.println("What would you like to do?");
 choice = input.nextLine();


}while(!choice.equals("enter building"));

我不确定你的确切要求,所以根据需要混乱的条件,但我认为方法应该在这里做。

在您的情况下,没有提到破坏条件,while(true){ //Infinite loop }这就是它无限运行的原因。

于 2013-07-31T05:14:41.330 回答
0
while (true) {
    System.out.println("You are now...");
    String choice = Scanner.nextLine();
    if (choice.equals("enter building")) break;
}
于 2013-07-31T05:07:47.533 回答
0

问题是你永远不会改变循环中的选择值。基本上,一旦他们做出选择,他们就必须永远做出选择。

choice = input.nextLine();

另外,您的循环将无限期地发生,因为它始终是正确的。您可能会中断以退出循环或使用变量来确定您的状态。

int state = 1;
while(state == 1) {
    choice = input.nextLine();
    if(choice.equals("leave")) {
        state = 2;
        System.out.println("Goodbye");
    }
}

每个好的游戏都有状态和某种有限状态机。

于 2013-07-31T05:09:18.420 回答
0

代替

        String choice = input.nextLine();
    while(true)
        if (choice.equals("enter building"))
            System.out.println("You have entered the empty building");

        else 
            System.out.println("You are now in the street with a dead body lying around drawned in blood \n there is a building right infront of you\n What would you like to do?");

while(!input.nextLine().equals("enter building")){
        System.out.println("You are now in the street with a dead body on the floor drowned in blood \n there is a building right ahead of you\n What would you like to do?");
    }
于 2013-07-31T05:20:27.777 回答