Results 1 to 8 of 8
  1. #1
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default How do you bring in integers from the main to another location

    My instructor didn't go over this and I am a bit lost, could someone out there explain to me how to bring in an int variable from the main to another location. I want to use currentRoll and lastRoll in my code below in a separate thing to calculate the score, you will see what I am trying to say when you look at my code. Thank you in advance.

    Java Code:
    import javax.swing.*;
    
    public class P_Final_Exam {
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?");
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
            int currentRoll = random();
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
            
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    int n = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]); 
    
    int lastRoll = currentRoll;
        }
    //public static void diceRoll(){
        //dice roll and guess
    
    
    
    //}
        
        //generate random number for die and icon
    public static int random(){
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int getScore(){
        //calculate score
        if (currentRoll == lastRoll){
            
        }
        return score;
    }
    public static int exit(){
        //display final score on exit
        
        }
    }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    If it's within the same class, just move it out of the constructor. I.e.:

    PHP Code:
    public class P_Final_Exam {
        int currentRoll;
        int lastRoll;
        public static void main(String[] args){
    You also need to change int currentRoll = random(); to currentRoll = random();, or it will create another int named currentRoll. Same with lastRoll.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    If you want to use variables from the main method in other methods, you must either pass those variables as parameters to the other methods, or make them as class member variables rather than local variables by declaring them in the class body outside any method. If you do the latter, you'll have to declare them 'static' because your methods are declared static.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Ah, yes, of course. I've gotten so used to creating constructors and calling them directly from the main() function it slipped my mind.

  5. #5
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you both for the quick replies, I will try that and see where it gets me and will let you know in a few min.

  6. #6
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    how do I declare them static for I am now getting a "non-static variable ..... cannot be referenced from a static context" error?

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Put the word "static" in front of "int".

  8. #8
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    that did the trick, now I have another question. Can you call a method to any other method other than the main? If so, how would you go about doing that? I have made a little change to my code and now have the following. Just trying to figure out the best way to do this without going too far beyond what my instructor has taught us. I need it to pretty much say in a new method that if currentRoll > lastRoll and the player clicked on the higher button to do the rightAnswer method.

    If you can point me to some examples if any would be great.


    Java Code:
    package Final;
    
    import javax.swing.*;
    
    public class P_Final_Exam {
        //declaring main variables
        static int currentRoll;
        static int lastRoll;
        static int score;
        
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?");
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
            currentRoll = random();
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
            
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    int n = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]); 
    
      lastRoll = currentRoll;
        }
    //public static void diceRoll(){
        //dice roll and guess
    
    
    
    //}
        
        //generate random number for die and icon
    public static int random(){
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int rightAnswer(){
        //add to score
        score++;
        return score;
    }
    public static int wrongAnswer(){
        //subtract from score
        score--;
        return score;
    }
    public static int sameAnswer(){
        //do nothing to score
        return score;
    }
    public static int exit(){
        //display final score on exit
        
        return exit();
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 09-02-2010, 10:43 PM
  2. Bring component to front of a container?
    By Cruncher in forum New To Java
    Replies: 9
    Last Post: 04-26-2010, 11:32 PM
  3. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  4. Bring back the GOTO ;-)
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 09-28-2009, 10:02 AM
  5. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •