If语句在满足条件时不输出变量

时间:2018-06-16 05:01:55

标签: java variables if-statement

当从JOptionPane.showInputDialog输入满足if语句条件时,我遇到以下代码没有输出JOptionPane.showMessageDialog变量输出的问题。如果不能满足条件或输出变量,我会非常喜欢帮助,我也尝试过println,它仍然会在每个showInputDialog中一个接一个地运行。提前谢谢。

public static void main(String[] args) {
    String findingCheese = "You arrive to a piece of day old cheese, the scent allures you to go closer";
    String findingCrack = "You arrive to a small crack in the wall, it looks like you could fit if you go closer";
    String walktoCrack = "You walk right up to the crack and realise you can fit in." + "\n";
    walktoCrack = walktoCrack + "You have your front legs in just as you hear someone right behind you";
    String eatCheese = "You walk up to the large piece of a cheese and can't resist the urge to eat it." + "\n"; 
    eatCheese = eatCheese + "As you are eating you here someone right behind you";
    String walkAway = "As you turn to run away, you realise the owner has been behind you this whole time, with one last wack your sight fades to black";
    String cheeseFall = "As you run pass the cheese, the monster in the kitchen slips and falls on the cheese, allowing you to escape. You have beaten the kitchen. Congrats";
    String youFit = "You charge into the crack realising you fit exactly, you esacpe as you hear the monster in the kitchen curse you. You have beaten the kitchen. Congrats";
    String story = "";
    story = story + "Welcome to the choose your own adventure horror story!" + "\n";
    story = story + "You are a rat stuck in a kitchen, inside this kitchen is someone that doesn't want you there." + "\n";
    story = story + "They want you dead! To escape, you must choose the correct directions and actions to take." + "\n";
    story = story + "Good luck rodent.";
    JOptionPane.showMessageDialog(null, story);
    String direction = JOptionPane.showInputDialog("Enter which direction you want to go between left or right");
    if ( direction == "Left" || direction == "left") {
    JOptionPane.showMessageDialog(null, findingCheese);
    }
    if ( direction == "Right" || direction == "right") {
    JOptionPane.showMessageDialog(null, findingCrack);
    }
    String closer = JOptionPane.showInputDialog("Do you want to go closer? yes or no");
    if ( closer == "yes" || closer == "yes" && direction == "Left" || direction == "left") {
    JOptionPane.showMessageDialog(null, eatCheese);
    }
    if ( closer == "yes" || closer == "yes" && direction == "Right" || direction == "right") {
    JOptionPane.showMessageDialog(null, walktoCrack);
    }
    if ( closer == "no" || closer == "No") {
        JOptionPane.showMessageDialog(null, walkAway);
    }
    String run = JOptionPane.showInputDialog("Do you want to run away right now? enter yes, if you want to continue forward enter no");
    if ( run == "yes" || run == "Yes") {
    JOptionPane.showMessageDialog(null, walkAway);
    }
    else if ( run == "no" || run == "No" && direction == "Left" || direction == "left") {
    JOptionPane.showMessageDialog(null, cheeseFall);
    }
    else if (run == "no" || run == "No" && direction == "right" || direction == "Right") {
    JOptionPane.showMessageDialog(null, youFit);
    }   

    }

0 个答案:

没有答案