我不确定一个人选择一个问题后如何进行

时间:2020-05-25 05:12:07

标签: repl.it

 System.out.println(" ");
System.out.println("I knew something was up driving past that billboard.");
System.out.println(" ");
System.out.println("Something didn’t feel right.");
System.out.println(" ");
System.out.println(sName + " " + "Watch out there's a deer!!!");
System.out.println(" ");
System.out.println("1) Turn to the left ");//Three options to choose from.
System.out.println("2) Turn to the right ");
System.out.println("3) Continue driving forward ");

int iAnswer = 0;// Need to put this above so it'll work.
iAnswer = Integer.parseInt(System.console().readLine()); // Finding when we're getting there
if (iAnswer == 1) {
  System.out.println("You skid to the left dramatically, and injure Keith badly.");
if (iAnswer == 2) {
  System.out.println("Skid to the right fast avoiding the deer and crash the car into a tree damaging" + " " + sName + ".");
if (iAnswer == 3){
  System.out.println("You hit the deer, and die of a fatal accident flipping the car and killing both your friends.");

    }
  }
}

例如,如果玩游戏的人选择1作为答案,从而导致Keith在车祸中受了重伤,我希望以后将此作为玩游戏的人的不利条件。我如何告诉PC从该答案开始遵循该特定路径。如果他们选择问题3导致所有角色死亡,那么我该如何结束比赛。

1 个答案:

答案 0 :(得分:0)

您可以在程序的开头创建一个布尔变量keithInjured,如果用户输入1,则将其设置为true,然后在程序中在相关时检查其值。

您可以将不同的执行路径放入方法中,并在if语句中调用正确的方法。

还要注意,您将最后2个if语句(用于检查输入是2还是3)包装到第一个中。如果第一个if语句的条件评估为false,则将不检查后两个,因为它们在第一个条件的范围内。在其代码块末尾关闭if语句的花括号。

boolean keithInjured = false;     
if (iAnswer == 1) {
          System.out.println("You skid to the left dramatically, and injure Keith badly.");
          keithInjured = true;
         path1();
}
if (iAnswer == 2) {
          System.out.println("Skid to the right fast avoiding the deer and crash the car into a tree damaging" + " " + sName + ".");
          path2();
}
if (iAnswer == 3){
          System.out.println("You hit the deer, and die of a fatal accident flipping the car and killing both your friends.");
          path3();
 }
//later in program
if (keithInjured) {
     //do something
}