简体   繁体   English

如何使我的代码与嵌套if语句一起使用

[英]How do I get my Code to work with nested if statements

I am creating a Interactive Story in Java. 我正在用Java创建一个交互式故事。 I am using Netbeans and Jswing to do all the GUI. 我正在使用Netbeans和Jswing来做所有的GUI。 I have an issue when nesting another if statement inside it doesn't allow the parameter... 当在其中嵌套另一个if语句时不允许使用参数...

if("Open Locker".equalsIgnoreCase(input)) {
     jLabel2.setText("You open your locker and tap your textbooks and they appear in your inventory. The warning bell rings.");
     jLabel3.setText("You remember your next class is World History...");

     if("Go to World History".equalsIgnoreCase(input)) {
          jLabel2.setText("You walk down the hallway realizing that you still have 45 minutes until");
          jLabel3.setText("class starts. You arrive in class and sit in your desk.");
     }
 }
 else {
     jLabel3.setText("You cannot do that right now.");
 }

Code Output 代码输出

I can type in open locker and it works but when I type in go to world history it doesn't work it says you cannot do this right now... 我可以输入打开的储物柜并且可以使用,但是当我输入进入世界历史记录不可用时,它表示您现在无法执行此操作...

I can type in open locker and it works but when I type in go to world history it doesn't work it says you cannot do this right now 我可以输入打开式储物柜,但可以,但是当我输入进入世界历史记录时,它不起作用,这表明您现在无法执行此操作

This happens because your input hasn't been changed. 发生这种情况是因为您的input没有更改。 To get into the first option you need to type Open Locker , then for the second you need to type Go to World History , but you already typed something in. 要进入第一个选项,您需要输入Open Locker ,然后输入第二个,您需要输入Go to World History ,但是您已经输入了一些内容。

What you need to do is ask for another input after you go into the first if statement: 您需要做的是在进入第一个if语句后要求其他输入:

if("Open Locker".equalsIgnoreCase(input)) {
     jLabel2.setText("You open your locker and tap your textbooks and they appear in your inventory. The warning bell rings.");
     jLabel3.setText("You remember your next class is World History...");

     //ask for input

     if("Go to World History".equalsIgnoreCase(input)) {
          jLabel2.setText("You walk down the hallway realizing that you still have 45 minutes until");
          jLabel3.setText("class starts. You arrive in class and sit in your desk.");
     }
}

You need keep track of last input. 您需要跟踪最近的输入。 Lets take variable isLockedOpened to do that 让我们使用变量isLockedOpened来做到这一点

boolean isLockedOpened=false;

Now if locker is open set isLockedOpened=true. 现在,如果更衣室处于打开状态,则设置isLockedOpened = true。 Next time when user inputs "Go to World History" then check input and isLockedOpened variable. 下次用户输入“转到世界历史记录”时,然后检查输入和isLockedOpened变量。 Your code looks something like this: 您的代码如下所示:

if("Open Locker".equalsIgnoreCase(input)) {
     //set locker opened
     isLockedOpened=true;
     jLabel2.setText("You open your locker and tap your textbooks and they appear in your inventory. The warning bell rings.");
     jLabel3.setText("You remember your next class is World History...");
 }else if("Go to World History".equalsIgnoreCase(input) && isLockedOpened=true) {
          jLabel2.setText("You walk down the hallway realizing that you still have 45 minutes until");
          jLabel3.setText("class starts. You arrive in class and sit in your desk.");
 }
 else {
     jLabel3.setText("You cannot do that right now.");
     //isLockedOpened=false;//you can set this to true or false but understand what will happen in both case.
 }

Hope this will provide you little hint !!! 希望这会给您一点提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM