【问题标题】:Calling a method to return a different int from a switch调用方法以从开关返回不同的 int
【发布时间】:2017-04-09 17:57:47
【问题描述】:

我正在尝试调用一个方法并根据输入将该值返回给我在另一个类中的开关。因此,如果我要在数组中选择“1”元素,我会将返回为“4”的回复返回到我的其他类并使其运行开关“4”。

在另一个类中,我将开关“4”设置为另一种方法。但是,我不断从我的方法返回 int 值“1”,因为它不断调用该方法(开关“1”)。这是有道理的,因为它是我数组中的“1”元素,然后它应该运行开关“1”,但我认为通过设置“reply = 4”,它会将一个 int“4”返回到我的另一个类中,从而调用切换“4”。

如何让我的方法返回我想要的值,以便我可以放入我的开关?

这是我的第一堂课:

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!!!");
}

}

还有我称之为的东西所在的第二个地方:

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;
}

}

【问题讨论】:

  • 返回reply变量的代码在哪里?
  • @GiorgosAltanis 实际上拥有它!对不起,它只是在一些注释掉代码后的底部。
  • 在一个类中声明的变量 (reply) 与在另一个类中声明的不同
  • 也就是说,您没有使用来自 springMeadow 的返回值 - 也许这适用于 reply = roomCall.springMeadow(); ,其他情况也一样

标签: java methods switch-statement


【解决方案1】:

您没有从 start() 方法返回 reply 值,因为该方法是 void 并且内部的 switch 语句不返回任何内容,尽管它们包含对诸如 int aerynsOverlook(). You should modifystart 等适当方法的调用这个:

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

其次,我看不到您如何调用start() 方法来获取其返回值并在第二个开关中使用它。你的MEssages 类与Rooms 没有任何联系

【讨论】:

  • 我确实拥有它!它位于一些注释掉的代码的底部。不过感谢您的收获。
  • 请更新您的代码,并向我们展示您如何使用第一个值调用第二个开关
  • 我添加了整个两个类,所以所有信息都在那里。我认为我的问题之一可能是我正在进行的 do-while 循环,它把所有东西都扔掉了。
  • 这似乎不是问题解决方案,更多的是返回值应该分配给第二个switch语句中使用的reply
  • 是的,我已经更新了我的答案,谢谢!该代码还有更多其他问题......
猜你喜欢
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多