简体   繁体   中英

How do I execute a certain code and then back at my main code

So I want to find a way to execute my code and then back to my main code. I'm new at java so please dont judge my bad code :) Anw so i have this piece of code and i want it to be activated only if the user inserts the number 2 at the code Below

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;
                }
            }

Below is here.. So this code should start the code above.. but it says that the variable CitizenPhase does not exist... Ik that this happens because this code is above the code above xD confused? So is there another way than using MainMenu=false; CitizenPhase=true; to start a piece of code? Thank you for your time :)

if (Buy1.equals("2")) {
                    MainMenu=false;
                    CitizenPhase=true;
                }

Move CitizenPhase declaration before the checking like:

Boolean CitizenPhase=false;
if (Buy1.equals("2")) {
   MainMenu=false;
   CitizenPhase=true;
}

while(CitizenPhase){...

We need more of the code. You are testing a conditional on Buy1 but it isn't declared in what you've shown us.

Why don't you use a method? ie:

public static void method1(){ //insert above code here }

if (Buy1.equals("2"){ method1();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM