Modify You
Modify You

Reputation: 153

Calling Certain Methods

import java.util.Scanner;

import javax.swing.JOptionPane;

public class HW {

public static void main(String[] args){
    while (retry == true){
        getGuess();
        getBet(balance);
        checkGuess(getGuess());
        updateBal(guessResult, betParsed);
        goAgain(balance);
    }
}


public static String getGuess(){
    //Guess input
    System.out.println("Guess: (H/T");
    Scanner in = new Scanner(System.in);
    boolean validInput = false;
    String guess = null;
    while (validInput == false){
        guess = in.next(); 
        if (guess.equals("H") || guess.equals("T")){
            validInput = true;
        } else {
            JOptionPane.showMessageDialog(null, "Invalid Input: " + guess);
        }
    }
    return guess;
}

public static boolean checkGuess(String sideGuess){
    //Checks if the side is correct
    double num = Math.round(Math.random());
    boolean guessResult;
    if (num == 0 && sideGuess.equals("H")){
        System.out.println("Correct. The side was: H");
        guessResult = true;
        return true;
    } else if (num == 1 && sideGuess.equals("T")){
        System.out.println("Correct. The side was: T");
        guessResult = true;
        return true;
    } else {
        System.out.println("Incorrect.");
        guessResult = false;
        return false;
    }
}

public static double getBet(double balance){
    //Retrieves a bet from the user
    Scanner in = new Scanner(System.in);
    String betInput = null;
    double betParsed = 0;
    boolean validInput = false;
    while (validInput == false){
        betInput = in.next();
        try {
            betParsed = Double.parseDouble(betInput);
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invlaid Input: " + betInput);
        }
        if (betParsed > balance || betParsed < 0){
            JOptionPane.showMessageDialog(null, "Invalid Input! You have: $" + balance);
        } else {
            validInput = true;
        }
    }
    return betParsed;
}

public static double updateBal(boolean checkGuess, double getBet){
    //Updates the balance based on the bet
    double balance = 0;
    if (checkGuess == true){
        balance = balance + getBet * 2;
        System.out.println("Your balance is now: $" + balance);
    } else {
        System.out.println("Your balance is now: $" + balance);
        balance = balance - getBet;
    }
    return balance;
}

public static boolean goAgain(double balance){
    //Determines if new play is needed
    Scanner in = new Scanner(System.in);
    boolean validInput = false;
    String goAgainInput = null;
    boolean retry;
    while (validInput == false){
        System.out.println("Go again? (Y/N)");
        goAgainInput = in.next();
        if (goAgainInput.equals("Y") || goAgainInput.equals("N")){
            validInput = true;          
        } else {
            JOptionPane.showMessageDialog(null, "Invalid Input: " + goAgainInput);
        }
    }
    if (goAgainInput.equals("Y")){
        retry = true;
        return true;
    } else {
        System.out.println("You ended with: $" + balance);
        retry = false;
        return false;
    }
}
}

Edited the code.

I'm trying to pass some defined variables into some of the methods, but it seems that I can't use them?

How can I fix this?

It seems they are not "local", which I cannot understand as they are defined in the methods. I must be under thinking it.

Upvotes: 1

Views: 91

Answers (1)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235984

The main() method should look like this, with only one parameter:

public static void main(String[] args) {

And for the second issue, updateBal() receives two parameters, but you're passing none. So the compiler is correctly complaining that you should pass them, according to what you want to do:

updateBal(false, 0); // pass the right values

Regarding the passing of parameters to the method, this is wrong:

getBet(balance);
checkGuess(getGuess());
updateBal(guessResult, betParsed);

You see, the variables guessResult and betParsed are local to the methods in which they were defined, you can't use them outside. And the methods return a value, which gets lost, because you're not using or storing it. Both problems have a simple solution - declare new variables local to the method:

double betParsed = getBet(balance);
boolean guessResult = checkGuess(getGuess());
updateBal(guessResult, betParsed);

Again, the same problem is here, in the main loop:

boolean retry = true;
while (retry) {
    // ...
    retry = goAgain(balance);
}

Bottom line: you must do something with the value returned by methods, and the variables declared inside a method will not be visible outside them.

Upvotes: 4

Related Questions