【问题标题】:I'm not sure how to carry on after a person selects one question一个人选择一个问题后,我不确定如何继续
【发布时间】:2020-09-11 17:22:01
【问题描述】:
 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 导致所有角色都死亡,我将如何结束游戏。

【问题讨论】:

  • 你已经得到了答案。您应该接受它,或者向作者解释为什么它不能解决您的问题。请记住,人们正在投入时间通过撰写答案来帮助您。

标签: repl.it


【解决方案1】:

您可以在程序的开头创建一个布尔变量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
}

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2011-11-05
    • 2022-11-03
    • 2012-12-15
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多