编译时没有收到任何错误,但是输出不正确? 用户应该输入时,程序将停止。
import java.util.Scanner;
class person
{
private String name, choice1, choice2, choice3;
//Getters
public String getName()
{
return this.name;
}
public String getChoice1()
{
return this.choice1;
}
public String getChoice2()
{
return this.choice2;
}
public String getChoice3()
{
return this.choice3;
}
//Setters
public void setName( String n )
{
this.name = n;
}
public void setChoice1( String c1 )
{
this.choice1 = c1;
}
public void setChoice2( String c2 )
{
this.choice2 = c2;
}
public void setChoice3( String c3 )
{
this.choice3 = c3;
}
}
public class AdventureGame
{
public static void main(String[] Args) throws Exception
{
String end = "Game Over";
Scanner keyboard = new Scanner(System.in);
person p = new person();
//intro
System.out.println( "Welcome travellar, your adventure awaits you..." );
Thread.sleep(1000);
System.out.print( "Player Name: " );
String inputName = keyboard.next();
p.setName(inputName);
Thread.sleep(1000);
//Question 1
System.out.println( "You are in a creepy house! Would you like to go \"upstairs\" or into the \"kitchen\"?" );
String inputChoice1 = keyboard.nextLine();
p.setChoice1(inputChoice1);
if ( p.getChoice1().equals("kitchen") )
{ //Q2
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 \"refridgerator\" or look in a \"cabinet\" ");
String inputChoice2 = keyboard.nextLine();
p.setChoice2(inputChoice2);
if ( p.getChoice2().equals("refridgerator") )
{
System.out.println("Inside the refridgerator you see some food. Would you like to eat that food? (\"yes\" or \"no\")");
String inputChoice3 = keyboard.nextLine();
p.setChoice3(inputChoice3);
if ( p.getChoice3().equals("yes") )
{
System.out.println(p.getName() + " died of food poisoning.");
Thread.sleep(1000);
System.out.print(end);
}
else if ( p.getChoice3().equals("no") )
{
System.out.print("You will never know what that food tasted like. The regret haunts you till suicide.");
Thread.sleep(1000);
System.out.print(end);
}
}
else if ( p.getChoice2().equals("cabinet") )
{
System.out.println( "The cabinet was a trap! You took a barbed contraption to the face; you are blinded and bleeding out." );
Thread.sleep(1000);
System.out.println("As you lie on the floor blind and bleeding out, you hear footsteps. Do you \"move\" or try to \"hide\"?");
String inputChoice3 = keyboard.nextLine();
p.setChoice3(inputChoice3);
if ( p.getChoice3().equals("move") )
{
System.out.print("As you moved faster so did the footsteps. you took a final blow.");
Thread.sleep(1000);
System.out.print(end);
}
else if ( p.getChoice3().equals("hide") )
{
System.out.println("blinded, you failed to realise that you were in plain sight. You became an easy meal.");
Thread.sleep(1000);
System.out.print(end);
}
}
}
else if ( p.getChoice1().equals("upstairs") )
{
System.out.println("As you reach the top of the stairs, your encounter 3 doors; which door do you enter? (\"1\" or \"2\")");
String inputChoice2 = keyboard.nextLine();
p.setChoice2(inputChoice2);
if ( p.getChoice2().equals("1") )
{
System.out.println("As you grab the handle of the first of the door you hear a scream!");
Thread.sleep(1000);
System.out.println("do you \"run\" or \"open\" the door?!");
String inputChoice3 = keyboard.nextLine();
p.setChoice3(inputChoice3);
if( p.getChoice3().equals("run") )
{
System.out.println("Running down the stairs, you misplaced a foot and fell to your demise.");
Thread.sleep(1000);
System.out.print(end);
}
else if (p.getChoice3().equals("open"))
{
System.out.println("As your head poked through, an unrelenting force slammed the door, decapitating your head.");
Thread.sleep(1000);
System.out.print(end);
}
}
else if ( p.getChoice2().equals("2") )
{
System.out.println("You enter what seems to be a vacant bedroom. Do you take a nap? (\"yes\" or \"no\")");
String inputChoice3 = keyboard.nextLine();
p.setChoice3(inputChoice3);
if ( p.getChoice3().equals("yes") )
{
System.out.println("You never wake up...");
System.out.print(end);
}
else if ( p.getChoice3().equals("no") )
{
System.out.println("You turn around turn around to leave bu the door is gone?!");
Thread.sleep(1000);
System.out.println( "The grim reaper appears... Your time has come." );
Thread.sleep(1000);
System.out.print(end);
}
}
}
}
}
答案 0 :(得分:4)
使用n_nodes = arr[0][0] # gets number of nodes, 3
first_section = [element[1] for element in arr]
和next()
可能会引起一些混淆。
nextLine()
,您正在扫描输入,直到下一个空格,然后将光标放置在扫描停止的位置next()
,您正在扫描输入,直到行尾,然后将光标放在新行上。免责声明 :在继续阅读并查看建议的解决方案之前,您可以尝试使用上述信息来找出代码的问题所在,并查看如果可以自己解决! :)
因此,轻微的 bug 发生在以下行:nextLine()
例如,如果我要在您的应用程序中键入String inputName = keyboard.next();
,然后按Enter,则apogee
将设置为inputName
。到目前为止似乎还不错,但是回车键也被视为输入。
现在,当我们到达apogee
时,回车键将被视为输入,而您的String inputChoice1 = keyboard.nextLine();
现在是一个空字符串。由于您没有匹配空字符串的条件,因此您将退出应用程序。
有两种方法可以解决此问题:
inputChoice1
之后添加keyboard.nextLine()
,以处理由回车键(“ \ n”)触发的输入String inputName = keyboard.next();
代替keyboard.nextLine()
快乐编码!