简体   繁体   中英

Calling a method to return a different int from a switch

I'm trying to call a method and based on the input, return that value to a switch I have in another class. So if I were to choose the "1" element in my array, I would get reply returned as "4" back into my other class and make it run switch "4".

In the other class I have the switch "4" set to another method. However, I keep getting the int value "1" returned from my method as it keeps calling that method (switch "1"). Which makes sense since it's the "1" element in my array, it should then run switch "1", but I thought that by setting "reply = 4", it would return an int "4" into my other class and thus invoke switch "4".

How do I get my methods to return the value I want so I can put into my switch?

Here's my first class where I'm doing the calling:

package bunnyhunt;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Messages {

//Objects 
public Rooms roomCall = new Rooms();

//Instance Variables
public static boolean gameOver = false;
public static int roomNum;
public static int reply;
boolean visitedOtherRoom = false;

//Constructors    
//Methods    
public void start() {

//while loop
    while (gameOver == false) {

//do-while loop
        do {
            String[] parkEntrance = {"Spring Meadow", "Aeryn's Overlook", "Garden Gate"};
            reply = JOptionPane.showOptionDialog(null, "You're at the Park Entrance! What do you want to do?", "Bunny Adventure", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, parkEntrance, parkEntrance[0]);

            switch (reply) {
                case 0:
                    System.out.println("oh man...Spring");
                    visitedOtherRoom = true;
                    roomCall.springMeadow();

                    break;
                case 1:
                    System.out.println("oh man...Aeryn");
                    visitedOtherRoom = true;
                    roomCall.aerynsOverlook();

                    break;

                case 2:
                    System.out.println("oh man...Garden");
                    visitedOtherRoom = true;
                    roomCall.gardenGate();

                    break;

                default:
                    System.out.println("error");

            }

        } while (visitedOtherRoom == false);

        switch (reply) {
            case 0:
                System.out.println("second 0!!!");
                visitedOtherRoom = true;
                roomCall.parkEntrance();

                break;
            case 1:
                visitedOtherRoom = true;
                System.out.println("yes man!");
                roomCall.aerynsOverlook();
                break;

            case 2:
                visitedOtherRoom = true;
                roomCall.gardenGate();

                break;

            case 3:
                visitedOtherRoom = true;
                roomCall.peacefulPond();

                break;

            case 4:
                visitedOtherRoom = true;
                roomCall.readingNook();

                break;
            default:
                System.out.println("default");

        }
    }

    JOptionPane.showMessageDialog(null,
            "Game Over!!!");
}

}

and my second where the things I call reside:

package bunnyhunt;

import javax.swing.JOptionPane;


public class Rooms {


public static int reply;

public int springMeadow() {

     String[] springMeadow = {"Park Entrance", "Reading Nook",                   "Search"};
    Messages.reply = JOptionPane.showOptionDialog(null, "You're in the    Spring Meadow! What do you want to do?", "Spring Meadow", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, springMeadow, springMeadow[0]);

    switch (reply) {
        case 0:

            reply = 0;

            break;
        case 1:

            reply = 4;
            break;

        case 2:

            JOptionPane.showMessageDialog(null,
                    "You searched!");

            break;

        default:
            System.out.println("error");

    }


    return reply;

}

public int aerynsOverlook() {
 String[] aerynsOverlook = {"Park Entrance", "Peaceful Pond",   "Search"};
    Messages.reply = JOptionPane.showOptionDialog(null, "You're in    Aeryn's Overlook! What do you want to do?", "Aeryn's Overlook", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, aerynsOverlook, aerynsOverlook[0]);

    return reply;

}

public int gardenGate() {

   String[] gardenGate = {"Park Entrance", "Rose Gardent", "Butterfly Garden", "Flowering Forest"};
     return reply;

}

public int readingNook() {

    String[] readingNook = {"Spring Meadow", "Search"};
    Messages.reply = JOptionPane.showOptionDialog(null, "You're in the Reading Nook! What do you want to do?", "Reading Nook", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, readingNook, readingNook[0]);
    return reply;

}

public int parkEntrance() {

    String[] parkEntrance = {"Spring Meadow", "Aeryn's Overlook", "Search"};
    Messages.reply = JOptionPane.showOptionDialog(null, "You're in the Park Entrance! What do you want to do?", "Park Entrance", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, parkEntrance, parkEntrance[0]);
    return reply;
}

public int peacefulPond() {

    String[] peacefulPond = {"Raven's Tower", "Aeryn's Overlook", "Search"};
    Messages.reply = JOptionPane.showOptionDialog(null, "You're in the Peaceful Pond! What do you want to do?", "Peaceful Pond", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, peacefulPond, peacefulPond[0]);
    return reply;
}

}

You are not returning a reply value from the start() method since this method is void AND switch statements inside don't return anything, despite they contain calls to appropriate methods like int aerynsOverlook(). You should modify int aerynsOverlook(). You should modify start` method like this:

public int start() {
    ...
    switch (reply) {
            case 0:
                System.out.println("oh man...Spring");
                visitedOtherRoom = true;
                return roomCall.springMeadow();
...

Secondly, I don't see how you are calling start() method to get its return value and use it in the second switch. Your MEssages class doesn't have any connection with the Rooms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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