所以我尝试调用方法,并且希望它能够从一种方法转到另一种方法。所以我一开始尝试用参数调用该方法,但这种方法不起作用,因为 1 个方法需要更多,并且在出现一些错误后我感到困惑,所以我尝试了这个...我在帖子上读到了一个答案,它说尝试在主方法之外但在类内部使用 static 声明变量。所以我这样做了
public class RiskGame {
//Player 1 Win
static Boolean P1Qrehz=false;
static Boolean P1Mez=false;
static Boolean P1Flehst=false;
static Boolean P1Veiled=false;
static Boolean P1Sharktooth=false;
static Boolean P1Zicat=false;
//Player 2 Win
static Boolean P2Qrehz=false;
static Boolean P2Mez=false;
static Boolean P2Flehst=false;
static Boolean P2Veiled=false;
static Boolean P2Sharktooth=false;
static Boolean P2Zicat=false;
//Stock Market
static Random stock = new Random();
static int StockCorn = stock.nextInt(433 - 350) + 350;
static int StockCotton = stock.nextInt(100 - 65) + 63;
static int StockOrange = stock.nextInt(125 - 112) + 112;
static int StockSilver = stock.nextInt(1350 - 1000) + 1000;
static int StockSugar = stock.nextInt(500 - 200) + 200;
static int StockCoffee = stock.nextInt(400 - 300) + 300;
static int StockRice = stock.nextInt(265 - 170) + 170;
static String NameP1;
static String NameP2;
static int MoneyCount;
static int MoneyCount2;
static int CitizenLoveNum2;
static int CitizenLoveNum;
static int SoldierCount1;
static int SoldierCount2;
static String Rules;
public static void main(String[] args) {
这是在 main 方法中
public static void main(String[] args) {
//Testing Area
//Player 1 Win
//This is the Give name phase
JOptionPane.showMessageDialog(null, "Welcome To RiskGame 0.1.1, This game is in Early access So expect some bugs! :)", "RiskGame 0.1.1", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "This game was originally developed and released By Elias Ioannou", "A Game By Elias Ioannou", JOptionPane.INFORMATION_MESSAGE);
String Name1 = JOptionPane.showInputDialog("What's your name Player 1?", "Player 1");
String Name2 = JOptionPane.showInputDialog("What's your name Player 2?", "Player 2");
Name1=NameP1;
Name2=NameP2;
//Money Setup
String Money = JOptionPane.showInputDialog(NameP1 + ", How Much Money do you want to start with?", "1000");
String Money2 = JOptionPane.showInputDialog(NameP2 + ", How Much Money do you want to start with?", "1000");
int MoneyCountP1 = Integer.parseInt(Money);
int MoneyCountP2 = Integer.parseInt(Money2);
MoneyCount=MoneyCountP1;
MoneyCount2=MoneyCountP2;
//Rules
String Rules1=JOptionPane.showInputDialog("This rules will add something more exciting in the game. (This rules must be done by the players). The system won't know anything so you need to play by your rules. \n Enter the custom rules you want to play with. If you want to just play the game leave it blank it won't affect anything!", "Default. No Special Rules Added");
Rules1=Rules;
//Soldier Count
int SoldierCountP1 = 3;
int SoldierCountP2 = 3;
SoldierCount1=SoldierCountP1;
SoldierCount2=SoldierCountP2;
JOptionPane.showMessageDialog(null, "You will Both Start With " + SoldierCount1 + " Soldiers!", "Soldiers On Arms", JOptionPane.INFORMATION_MESSAGE);
//Starting City
Random city = new Random();
int City = city.nextInt(3 - 1) + 1;
Random city2 = new Random();
int City2 = city2.nextInt(3 - 1) + 1;
int CityNum = 0;
switch (City) {
case 1:
JOptionPane.showMessageDialog(null, NameP1 + ", Your Starting Island is Veiled Atoll");
P1Veiled=true;
CityNum = 1;
break;
case 2:
JOptionPane.showMessageDialog(null, NameP1 + ", Your Starting Island is Skarktooth Isle");
P1Sharktooth=true;
CityNum = 2;
break;
case 3:
JOptionPane.showMessageDialog(null, NameP1 + ", Your Starting Island is Zicat Peninsula");
P1Zicat=true;
CityNum = 3;
break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong... Try Restarting the program.");
break;
}
int City2Num = 0;
switch (City2) {
case 1:
JOptionPane.showMessageDialog(null, NameP2 + ", Your Starting Island is Mez Archipelago");
P2Mez=true;
City2Num = 1;
break;
case 2:
JOptionPane.showMessageDialog(null, NameP2 + ", Your Starting Island is Flehst Archipelago");
P2Flehst=true;
City2Num = 2;
break;
case 3:
JOptionPane.showMessageDialog(null, NameP2 + ", Your Starting Island is Qrehz Archipelago");
P2Qrehz=true;
City2Num = 3;
break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong... Try Restarting the program.");
break;
}
//Citizen Happiness
String CitizenLove = JOptionPane.showInputDialog(NameP1+", How Much Citizen Happiness do you want to start with?", "50");
int CitizenLoveNumP1= Integer.parseInt(CitizenLove);
String CitizenLove2 = JOptionPane.showInputDialog(NameP2+", How Much Citizen Happiness do you want to start with?", "50");
int CitizenLoveNumP2= Integer.parseInt(CitizenLove2);
CitizenLoveNum2=CitizenLoveNumP2;
CitizenLoveNum=CitizenLoveNumP1;
P2Menu();
}
这是我尝试调用但没有响应的方法(程序退出)
public static void P2Menu(){
//-CitizenLove
Boolean Yes=true;
while(Yes){
CitizenLoveNum2-=15;
}
怎么了?我是 java 新手,所以请不要讨厌我可能很糟糕的编码技能:)
最佳答案
要在您所在的类之外调用静态方法,您需要执行以下操作:
RiskGame.P2Menu();
您还需要确保在 RiskGame
类中定义 P2menu()
方法,如下所示:
public class RiskGame {
....
public static void main(String[] args) {
...
P2Menu();
}
public static void P2Menu(){
...
}
此外,P2Menu()
方法存在问题,因为它处于无限循环中。您从未将变量设置为false
。
关于java - 我在这里做错了什么?方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31442860/