Results 1 to 2 of 2
  1. #1
    biodunalfet is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default problem with using a method's object/variable in another method. Please help!!!

    Pls check these codes for me. The variable 'answer' gives zero when called in method run and i need it to retain its value throughout the programme.
    The programme generates a number with d method generate() and the method run() checks if the user is right or wrong. If wrong it allows the user to re enter until he gets it. Bt like i said the variable 'answer' is giving me problems


    import java.util.Random;
    import java.util.Scanner;

    public class RandomGame{


    public static Random nw= new Random();

    static String outcome;
    static int answer;
    static int gap;
    static int guess;
    static int realAns;

    public static void run()
    {
    System.out.println(answer);
    System.out.println("ENTER ANY NUMBER FROM 1 TO 10");

    Scanner input = new Scanner(System.in);
    int guess= input.nextInt();

    System.out.println(guess);

    int gap= answer-guess;

    if ((gap<=-3)||(gap>=3) )
    {
    outcome="Too far. Try again";
    }
    else
    {
    outcome="Very close. Try again";
    }

    if (guess==answer)
    {
    System.out.println("CONGRATULATIONS. YOU GUESSED THE NUMBER!");
    }

    else
    {
    System.out.println(outcome);
    run();
    }

    }

    public static int generate()
    {
    int answer= 1+ nw.nextInt(10);
    System.out.println(answer);
    return answer;
    }

    }

    the test package is
    public class test run{
    public static void main(String args())
    {
    RandomGame ne= new RandomGame();
    ne.generate(),
    ne.run();
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: problem with using a method's object/variable in another method. Please help!!!

    The programme generates a number with d method generate()
    If we look at the method in question:

    Java Code:
    public static int generate()
    {
        int answer= 1+ nw.nextInt(10);
        System.out.println(answer);
        return answer;
    }
    we see that it returns a randomly generated method to the caller, the main() method of another class and that's all. In particular it does not set the value of answer within the RandomGame class. There are two variables here: the static int of the RandomGame class and the local variable within the generate() method. The one in the method is said to shadow the one in the class.

    -----

    You should use standard Java coding conventions: classes begin with a capital letter and variables should be descriptive of what the variables are.

    Consider removing all static variables and methods. Nothing need be static except the main() method of the driver program.

Similar Threads

  1. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  3. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 PM
  4. how to use a variable of one method in another method
    By lucasautomacao in forum New To Java
    Replies: 4
    Last Post: 10-29-2008, 02:21 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

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