我正在尝试调用一个方法并根据输入将该值返回给另一个类中的开关。因此,如果我在我的数组中选择“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;
}
}
答案 0 :(得分:0)
您没有从reply
方法返回start()
值,因为此方法无效且内部switch
语句不会返回任何内容,尽管它们包含对适当方法的调用像这样的int aerynsOverlook(). You should modify
start`方法:
public int start() {
...
switch (reply) {
case 0:
System.out.println("oh man...Spring");
visitedOtherRoom = true;
return roomCall.springMeadow();
...
其次,我不知道你是如何调用start()
方法获取其返回值并在第二个开关中使用它。您的MEssages
课程与Rooms