简体   繁体   中英

Cannot find symbol calling a method

So I have a simple question not sure why this isn't working.

So I am making a game called Roulette and I want to have the user choose their starting balance. So after they open the game it creates a welcome message and tells them how to play. Then ask for the balance they want:

System.out.println("Welcome to Roulette! \nTo play please enter 1 to guess low (1-18) or 2 to guess high (19-36)");
userBalance = rps.getBalance();

So That is what's in my driver class that has access to another file with a whole bunch of methods (or will).

So I now I have this called in my other class called rps.

  public int getBalance(Scanner in, int userBalance)  
  {
     System.out.println("How much money would you like to play with?");
     userBalance = in.nextInt();

     while(userBalance > 0)
     {
        System.out.println("Invalid input, try again!");
        userBalance = in.nextInt();
     }

     return userBalance;
  } //end of UserBalance

Tell's me it cannot find symbol .getBalance

your getBalance function is taking two parameters, scanner object and int value userBalance but while calling this function userBalance = rps.getBalance(); you are not passing any parameters. Either remove those parameters from function prototype public int getBalance(Scanner in, int userBalance) or pass the required parameters in to this function while calling it.

You need to change your function getBalance . As it is returning userBalance, you don't need to pass getBalance value in it as parameter. Also you don't need to pass Scanner object in it as parameter. you can declare scanner object inside function body.

Try following code.

public int getBalance()  
  {
     Scanner in= new Scanner (System.in);
     int userBalance;
     System.out.println("How much money would you like to play with?");
     userBalance = in.nextInt();

     while(userBalance > 0)
     {
        System.out.println("Invalid input, try again!");
        userBalance = in.nextInt();
     }

     return userBalance;
  } //end of UserBalance

Now call this function, it will return int value.

int userBalance = rps.getBalance();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM