Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
Basically I want the JOptionPane "//main window" to take me back to the start but not to open a new instance of the program. see the red
Code:
	//start
	String name=JOptionPane.showInputDialog(null,"Enter your name");
	JOptionPane.showMessageDialog(null,"Welcome " +name);
				
	//move(main window)
	String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
	int go = Integer.parseInt(move);
				
	//SHOP
	if(go==1)
	{
					
		String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
		int sell=Integer.parseInt(sell1);
		//sell right
		if(sell==1)
		{
		JOptionPane.showMessageDialog(null,"1");	
		}
		//sell left
		else if (sell==2)
		{
		JOptionPane.showMessageDialog(null,"2");
		}
					
		[COLOR="Red"]//main window
		else if (sell==3)
		{
		//go(main window)
		 move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
		 go = Integer.parseInt(move);[/COLOR]
 

Kunimodi

macrumors member
Sep 8, 2006
65
0
Ashland, OR, USA
Hi, Jasonbot. There are several ways that you could structure your program to return to the main menu. Probably the way that would involve the least amount of change to your program would be to add the following to put a loop around the main menu handler with the [optional] addition of an exit option:

Code:
do
{
    String move=JOptionPane.showInputDialog(null, "1=Shop"+"\n2=Playground" +"\n3=Buy stock\n4=Exit");
    int go = Integer.parseInt(move);
    if (go == 1)
    {
        // shop handler
    } else if (go == 2) {
       // and so on
   }
} while (go != 4);
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
WOW! It works, can't believe it!!!! Can you maybe explain what this loop is doing. My understanding is that it loops until go is not equal to 4??? why 4?
 

Kunimodi

macrumors member
Sep 8, 2006
65
0
Ashland, OR, USA
Jasonbot said:
WOW! It works, can't believe it!!!! Can you maybe explain what this loop is doing. My understanding is that it loops until go is not equal to 4??? why 4?

Actually, I had a bug :) 'int go' should be declared before the loop or it won't be visible to the while clause (e.g. int go; do { } while (go != 4); }

I added 4 to the input dialog as the number to exit the program. Yes, it runs until go is 4.
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
Ok, cool! but now I can't leave shop to go to playground! I put the do inside the if, is that a problem? I tried with do instide if but then you can't get to one or the other!

Code:
//move(main window)
				String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
				int go = Integer.parseInt(move);
				
				if(go==2)
				{
				do{
				//Playground (find stuff)
				
				
					String play1=JOptionPane.showInputDialog(null,"In the playground" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int play=Integer.parseInt(play1);
					
					//play left
					if(play==1)
					{
						JOptionPane.showMessageDialog(null,"1");
					}
					
					//play right
					else if(play==2)
					{
						JOptionPane.showMessageDialog(null,"2");
					}
					
					//main menu
					else if(play==3)
					{
						//go(main window)
						move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
					}
					
					
				}while(go != 4);
				}
				
				if(go==1)
				{
				do{
				//SHOP
					String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int sell=Integer.parseInt(sell1);
					
					//sell right
					if(sell==1)
					{
					JOptionPane.showMessageDialog(null,"1");
					}
					
					//sell left
					else if (sell==2)
					{
					JOptionPane.showMessageDialog(null,"2");
					}
					
					//main window
					else if (sell==3)
					{
						//go(main window)
						move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
						
					}
					
					
				}while(go != 4);
				}
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
the error on that code is clearly a so called "logical" issue, where the code is like this:

Code:
   if num = 2
      do until num is 4
   (num is here 1 or 4)
   if num is 1 (first part of code haven't occured)
       do until code is 4
   (num here should be 4)

as you can see this is not optimal.

what you can do is the following:
Code:
class test {
   private int currentCommand;
   // simply all numbers and other variables here
   // you reach em by writing this.*name of variable*
   public void shop() {
      if (this.currentCommand == 1) {
          // do first part of code. end it with "break"
      } else if(currentCommand == 2) {
          // do second part of code here
      } else {
          // neither occurs for unknown reason.
          // reset to first command
          this.currentCommand = 1;
      }
      if(this.currentCommand != 3) {
          // if command is 3 this is not executed
          // we call back to this function if command
          // isn't 3.
          // 3 == application stops
          this.shop()
       }
   }
}

a better way to code this imo.

Code:
move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);

you should use a while loop such as this:

Code:
   if(num == 4) {
       do {
           //stuff
       } while(num == 4);   // when someone don't want to be here he'll leave
          // if he wants to stay he simply presses same direction again (4)
          // which seems to have been your intention with the dialogue
   }
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
oh Kunimodi had already helped you, jason, you just seem to have placed the while loop in the wrong place.

it's like this:

Code:
do
    what I want
while I still want to do something

this is what the do/while loop does, written in plain english.
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
I'll try it tomorrow, but before then I just need some more basic terminology:


public void shop() <--???

this.currentCommand <---was this just a variable name? or is this. important?

OK, it's still not tomorrow but I tried the *plain english* alternative, my code now only goes to the first option AKA only if you input "1" in the Input dialog. If you try "2" or "3" it dies :(

Code:
				//move(main window)
				String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
				int go = Integer.parseInt(move);
				
				do{
					if(go==1)
					{
				//SHOP
					String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int sell=Integer.parseInt(sell1);
					
					//sell right
					if(sell==1)
					{
					JOptionPane.showMessageDialog(null,"1");
					}
					
					//sell left
					else if (sell==2)
					{
					JOptionPane.showMessageDialog(null,"2");
					}
					
					//main window
					else if (sell==3)
					{
						//go(main window)
						move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
						
					}
					
					
					}}while(go != 4);
				do{
				//Playground (find stuff)
					
					if(go==2)
					{
						
						String play1=JOptionPane.showInputDialog(null,"In the playground" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
						int play=Integer.parseInt(play1);
						
					//play left
						if(play==1)
						{
							JOptionPane.showMessageDialog(null,"1");
						}
						
					//play right
						else if(play==2)
						{
							JOptionPane.showMessageDialog(null,"2");
						}
						
					//main menu
						else if(play==3)
						{
						//go(main window)
							move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
							go = Integer.parseInt(move);
						}
						
						
					}}while(go != 4);

				 do{
				//BuyStock(buy stuff to sell)
					 
					 if(go==3)
					 {
						 
						 String play1=JOptionPane.showInputDialog(null,"At the shop" +"\n1=Buy Food" +"\n2=Buy drink" +"\n3=Main Menu");
						 int play=Integer.parseInt(play1);
						 
					//buy drink
						 if(play==1)
						 {
							 JOptionPane.showMessageDialog(null,"1");
						 }
						 
					//buy food
						 else if(play==2)
						 {
							 JOptionPane.showMessageDialog(null,"2");
						 }
						 
					//main menu
						 else if(play==3)
						 {
						//go(main window)
							 move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
							 go = Integer.parseInt(move);
						 }
						 
						 
					 }}while(go != 4);
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
the currentCommand is declared outside of the method (the uppermost part of the code, "private int currentCommand") and is a so called global variable, which can be reached by any method within the overlaying class (in this case the class is "test")

public void shop() is a so called method in the class test. The name of the method is shop(), void is what it returns (void means nothing) and public means any code or application, even ones outside of the class, can reach it.

Code:
test something = new test();
// creates a new instance of "test"
something.shop() 
// code within "something" is called.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
This is probably what you are looking for:

Code:
public class MyApp {

    private static String NAME_MESSAGE = "Enter your name:";
    private static String MOVE_MESSAGE = "1=Shop\n2=Playground\n3=Buy stock";

    private String getUsername() {
        return JOptionPane.showInputDialog(null, NAME_MESSAGE);
    }

    private int getMove() {
        String move = JOptionPane.showInputDialog(null, MOVE_MESSAGE);
	return Integer.parseInt(move);
    }

    private void doShop() {
        System.out.println("Shopping");
        // do shopping stuff here.
    }

    private void doPlay() {
        System.out.println("Playing");
        // do playing stuff here.
    }

    private void doBuyStock() {
        System.out.println("Buying Stock");
        // do stock stuff here.
    }

    private void run() {
        //start
	String name=getUsername();

	JOptionPane.showMessageDialog(null,"Welcome " + name);

        boolean continueMoving = true;
        while(continueMoving) {
            int move = getMove();

            switch(move) {
                case 1:
                    this.doShop();
                    break;
                case 2:
                    this.doPlay();
                    break;
                case 3:
                    this.doBuyStock();
                    break;
                case 4:
                    continueMoving = false;
                    break;
                default:
                    // This is what happens if move != 1, 2, 3, or 4.
                    break;
            }
        }

    }

    public static void main(String[] argv) {
        MyApp instance = new MyApp();
        instance.run();
    }

}
 

Jasonbot

macrumors 68020
Original poster
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
I do want code like that :D but instead of printing lines I want to get values and use them (variables) here's a diagram of what needs to happen:

Main is the root selector:
Code:
String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
				int go = Integer.parseInt(move);

options is the options:
Code:
String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int sell=Integer.parseInt(sell1);

and more is the variables that will come out of the option selected, I'll use a random number(1,2 or 3) to get an "item" for the "inventory" but the main thing is that it must be able to go back to the beginning when you want to.


optionsye7.jpg
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
well we've all made code example of exactly what you describe, read through em, ask questions if you like - we're here and both ready and willing to help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.