【发布时间】: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