Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Passing a value to another class

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing a value to another class

    To summarise I am required to make a game, that a user has to guess the secret number. The secret number is generated by a random number.

    Anyway I'm having a problem passing the random number to another class. Currently I am only passing it to the main class for testing purposes.

    With this code the random number passes a zero
    import java.util.Scanner;
    import java.util.Random;
     
    public class GameOptions {
     
        Scanner scan = new Scanner(System.in);
        public int SecretNumber;
        public int RandomNumber;
     
        public void ShowSecretNumber() {
     
            do {
                System.out.println("Enter a secret number between 1 and 10");
                SecretNumber = scan.nextInt();
            } while (SecretNumber < 1 || SecretNumber > 10);
     
        }
     
        public void SetRandomNumber (int RandomNumber)
        {
            Random number = new Random();
            RandomNumber = number.nextInt(10) + 1;
        }
     
        public int getRandomNumber() {
            return RandomNumber;
        }
    }


    And with this it passes whatever the value I set it to. Currently a 5.

    import java.util.Scanner;
    import java.util.Random;
     
    public class GameOptions {
     
        Scanner scan = new Scanner(System.in);
        public int SecretNumber;
        public int RandomNumber;
     
        public void ShowSecretNumber() {
     
            do {
                System.out.println("Enter a secret number between 1 and 10");
                SecretNumber = scan.nextInt();
            } while (SecretNumber < 1 || SecretNumber > 10);
     
        }
     
        public void SetRandomNumber (int RandomNumber)
        {
            Random number = new Random();
            RandomNumber = number.nextInt(10) + 1;
        }
     
        public int getRandomNumber() {
            return RandomNumber=5;
        }
    }

    I can understand that RandomNumber=5 sets the value to 5 but why is this ignoring what the "SetRandomNumber" method sets the value to.

    Any ideas??
    Thanks in advanced


    EDIT: Just adding the main class in case someone needs to see the output..

     
    import java.util.Scanner;
     
    public class GameInterface {
     
        Scanner scan = new Scanner(System.in);
        public int lives;
     
        public static void main(String[] args) {
     
            //******//
            GameOptions number = new GameOptions();
            //******//
     
            int mode = 0;
            int choice;
            int lives;
            int secretnumber;
     
            Scanner scan = new Scanner(System.in);
            Statistics statistics = new Statistics();
            GameOptions secretNumber = new GameOptions();
            NumberGuessingGame game = new NumberGuessingGame();
     
            do {
                System.out.println("Select the mode of play: 1 Normal; 2 Testing");
                mode = scan.nextInt();
            } while (mode != 1 && mode != 2);
     
     
            do {
                System.out.println("*****MAIN MENU*****");
                System.out.println("1. Start a new game");
                System.out.println("2. Show the Stats");
                System.out.println("3. Exit");
                choice = scan.nextInt();
            } while (choice != 1 && choice != 2 && choice != 3);
     
            switch (choice) {
                case 1:
                    System.out.println("Enter number of lives");
                    lives = scan.nextInt();
     
                    System.out.println("Random Number = " + number.getRandomNumber());
     
                    if (mode == 2) {
                        secretNumber.ShowSecretNumber();
                    }
                    game.Play();
                    break;
                case 2:
                    statistics.ShowStatistics();
                    break;
                case 3:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Choose Again");
            }
        }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Passing a value to another class

    From what i can see, no need for parameter for your setRandomNumber method:
    public void SetRandomNumber()
        {
            Random number = new Random();
            RandomNumber = number.nextInt(10) + 1;
        }

    You're returned a 0 because you dont call setRandomNumber() in your main method, so it returns a default int value of 0 because it hasn't been initialised.

    You are returned 5 because you're assigning the value 5 each time you request the number, so it's always going to be 5.

    Call setRandomNumber in main before trying to call the GET method.

Similar Threads

  1. [SOLVED] Class not passing information
    By DiPep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 26th, 2010, 10:04 AM
  2. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM
  3. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  4. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  5. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM