我在这做错了什么?方法不起作用

时间:2015-07-15 23:19:20

标签: java

所以我试图调用方法,我希望它喜欢从一种方法转到另一种方法。所以我起初尝试用参数调用方法,但是那种方法不起作用,因为1方法需要更多,我在一些错误后感到困惑,所以我尝试了这个...我在帖子上读了一个答案,并说它试着宣布变量与main方法之外的静态但在类中。所以我做了它

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) {

这是主要方法

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新手所以请不要讨厌我可能不好的编码技巧:)

1 个答案:

答案 0 :(得分:0)

要在您所在的课程之外调用静态方法,您需要执行以下操作:

RiskGame.P2Menu(); 

您还需要确保在P2menu()类中定义RiskGame方法,如下所示:

public class RiskGame {
   ....
public static void main(String[] args) {
    ...
   P2Menu(); 
 }
public static void P2Menu(){
...
}

此外,P2Menu()方法存在问题,因为它处于无限循环中。您永远不会将变量设置为false