我想调用一个方法并基于输入,将该值返回给我在另一个类中的交换机。因此,如果我要选择数组中的“1”元素,我会将答复作为“4”返回到其他类中,并使其运行开关“4”。调用一个方法从交换机返回一个不同的int
在另一个类中,我将开关“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实际上有它!对不起,这只是在一些注释掉代码后的底部。 – mbarq
在一个类中声明的变量('reply')与另一个类 –