views:

61

answers:

4

I am trying to make a lottery game in Java and it uses method calls instead of while loops for looping purposes. However, I cannot get an input variable( such as int bet = input.nextInt()) to be recognized as a variable for other methods. I do not know how to globalize "bet" so it can be used by all the methods instead of just in the method it is a part of. Here is part of my code below, including just the one method called "play()"

public static void play()
{
    System.out.println("\nPlay? Yes = 1 No = 2 ");
    Scanner input = new Scanner(System.in);
    int play = input.nextInt();
    if(play == 1)
    {

        System.out.print("Current balance is " + account);
        System.out.print("\nPlace your bet: ");
        int bet = input.nextInt();

        if((bet <= account) && (bet > 0)){
            lottery();
        }
        else if((bet < 0) || (bet > account)){
            play();
        }
    }
    else if(play == 2){
        System.out.println("Final balance is " + account);
        System.exit(0);
    }
    else{
        System.out.println("Invalid input!");
        play();
    }

}
+1  A: 

Define bet outside of the play() method.

matt b
A: 

Pass it between methods as a parameter. (Defining it as public is not recommended.) Or, if this is a class, you can make it a member variable (property).

froadie
how do I pass it between methods? Are you talking about arguments? I tried that, but i cannot get it to work. And it is not a class, but the methods are in the class "lotterygame"
David
Yes, parameters == arguments. Try reading up on it in the Java Tutorials: http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html
froadie
+2  A: 

Simplest answer is to declare play and bet as static class variables.

public class Game {
   private static int play = 0;
   private static int bet = 0;

   public static void play() {
      System.out.println("\nPlay? Yes = 1 No = 2 ");
       Scanner input = new Scanner(System.in);
       play = input.nextInt();
       if(play == 1) {

        System.out.print("Current balance is " + account);
        System.out.print("\nPlace your bet: ");
        bet = input.nextInt();

        if((bet <= account) && (bet > 0)) {
            lottery();
        }
        else if((bet < 0) || (bet > account)){
            play();
        }
    }
    else if(play == 2){
        System.out.println("Final balance is " + account);
        System.exit(0);
    }
    else{
        System.out.println("Invalid input!");
        play();
    }
}
John Engelman
Thank you everyone for helping me. That is exactly what I was looking for. I tried to globalize it earlier, but i didnt realize that i needed to remove the int from the "int bet = input.nextInt();"
David
+1  A: 

If you define bet as a field in the class like :

public class Lottery {
    private int bet;

    public void lottery() {
        ...
    }

    public void play() {
        ...
    }

    public static void main(String[] args) {
        Lottery lottery = new Lottery();
        lottery.play();
    }
}

then bet is available in all methods in the class, but not outside.

It is not necessary to make play public, so this can stay nicely inside the scope of the play method. It is considered good practice to give variables no more visibility than strictly needed.

Most people would in this case not make bet a field, but pass it as a parameter to the lottery method. In this case the visibiltiy can even be further restricted to only the play and lottery methods.

A final note is you use of function calls to loop. Since java does not support tail-call-optimisation (and it would not apply here in any case) you are going to fill up the stack and finally die of a stack overflow if you play long enough.

Peter Tillemans
+1 For dropping the `static`
Ishtar