gpt4 book ai didi

java - 有时需要在扫描仪中多次输入输入才能工作?

转载 作者:行者123 更新时间:2023-12-01 11:35:55 25 4
gpt4 key购买 nike

嗨,我对编程很陌生。我最近开始了一个文本“逃离房间”类型的游戏,当我运行它时,它无法正常工作。我必须多次输入输入才能真正运行下一件事。

import static java.lang.System.*;
import java.util.Scanner;

public class RoomEscape {
public static void main(String args[]) throws InterruptedException{

Scanner scan = new Scanner(System.in);

out.println("Welcome to Room Escape.");
out.println("Please type \"start\" to continue");
String sta = scan.nextLine();
if (sta.equals("start"))
game();
}

public static void game() throws InterruptedException {
boolean run = true;
int ori = 1;
Scanner scan = new Scanner(System.in);
String ew = scan.nextLine();
String input = scan.next();

out.println("You wake up in a room.");
Thread.sleep(1000);
out.println("You don't know how you got there.");
Thread.sleep(1000);
out.println("You feel dizzy, tired, and hungry.");
Thread.sleep(1000);
out.println("Your objective is to leave the room");
Thread.sleep(1000);
out.println("Type \"help\" for your commands and inventory at any time.");

while (run) {
if (input.equalsIgnoreCase("help")) {
out.println("Commands are: \"right\" to turn right");
out.println("\"left\" to turn left");
out.println("\"examine\" to look more closely at an object");
out.println("\"pick up\" to put an object in your inventory");
}
else if (input.equalsIgnoreCase("right")) {
ori++;

}

}
}}

运行上面的代码时,有时需要输入两到三次“start”才能运行 game()。感谢您的帮助!

最佳答案

你的问题是,当你在 game() 中时,你会在游戏开始之前扫描输入,而不是在主循环内。你有

String ew = scan.nextLine();
String input = scan.next();

我不确定你为什么要使用ew

直接放

String input = scan.nextLine();

在 while 循环的开头并将它们从游戏函数的开头取出。

关于java - 有时需要在扫描仪中多次输入输入才能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29996705/

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