所以我想找到一种方法来执行我的代码然后回到我的主代码。我是java的新手,所以请不要判断我的错误代码:) Anw所以我有这段代码,我希望只有当用户在代码下面插入数字2时才激活它
Boolean CitizenPhase=false;
while(CitizenPhase){
JOptionPane.showMessageDialog(null, "Welcome To our Market. I see you need resources to please your citizens. Well You came to the right place!", "Global Market", JOptionPane.INFORMATION_MESSAGE);
String MarketBuy=JOptionPane.showInputDialog("Choose What you want to buy \n 1.Corn ("+StockCorn+"$) Increases Hapiness by 40 \n 2.Cotton ("+StockCotton+"$) Increases Hapiness by 10 \n 3.Oranges ("+StockOrange+"$) Increases Hapiness by 15 \n 4.Silver ("+StockSilver+"$) Increases Hapiness by 100 \n 5.Sugar ("+StockSugar+"$) Increases Hapiness by 45 \n 6.Coffee ("+StockCoffee+"$) Increases Hapiness by 35 \n 7.Rice ("+StockRice+"$) Increases Hapiness by 25", "Global Market");
if(MarketBuy.equals("1")){
MoneyCount-=StockCorn;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}if(MarketBuy.equals("2")){
MoneyCount-=StockCotton;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}if(MarketBuy.equals("3")){
MoneyCount-=StockOrange;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}if(MarketBuy.equals("4")){
MoneyCount-=StockSilver;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}if(MarketBuy.equals("5")){
MoneyCount-=StockSugar;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}if(MarketBuy.equals("6")){
MoneyCount-=StockCoffee;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}if(MarketBuy.equals("7")){
MoneyCount-=StockRice;
JOptionPane.showMessageDialog(null,"Buy Successfull. You now have "+MoneyCount+"$ left!", "Global Market",JOptionPane.INFORMATION_MESSAGE);
CitizenPhase=false;
MainMenu=true;
}
}
下面是这里..所以这段代码应该启动上面的代码..但是它说变量CitizenPhase不存在...我发现这是因为这段代码高于xD上面的代码混淆了?那么还有另一种方法,而不是使用MainMenu = false; CitizenPhase = TRUE;开始一段代码?谢谢您的时间:))
if (Buy1.equals("2")) {
MainMenu=false;
CitizenPhase=true;
}
答案 0 :(得分:1)
在检查之前移动CitizenPhase
声明:
Boolean CitizenPhase=false;
if (Buy1.equals("2")) {
MainMenu=false;
CitizenPhase=true;
}
while(CitizenPhase){...
答案 1 :(得分:0)
我们需要更多代码。您正在Buy1
测试条件,但它并未在您向我们展示的内容中声明。
答案 2 :(得分:0)
为什么不使用方法?即:
public static void method1(){
//insert above code here
}
if (Buy1.equals("2"){
method1();}