简体   繁体   中英

Variable Resetting

import java.util.Scanner;

import javax.swing.JOptionPane;

public class HW {

public static void main(String[] args){
    balance = 100;
    boolean goAgain = true;
    while (goAgain == true){
        checkGuess(getGuess(), getBet(balance));
        goAgain = goAgain();
    }
}

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

public static double getBet(double balance){
    Scanner in = new Scanner(System.in);
    String betInput = null;
    double betParsed = 0;
    boolean validInput = false;
    while (validInput == false){
        System.out.println("Bet? You have: $" + balance);
        betInput = in.next();
        try {
            betParsed = Double.parseDouble(betInput);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid Input: " + betInput);
        }
        if (betParsed > balance || betParsed <= 0){
            JOptionPane.showMessageDialog(null, "Invalid Input: " + betParsed);
        } else {
            validInput = true;
        }
    }
    return betParsed;
}
public static boolean checkGuess(String getGuess, double getBet){
    double num = Math.round(Math.random()*10);
    boolean correctSide = false;
    if (num <=5 && getGuess.equals("H")){
        correctSide = true;
    } else if (num >=6 && getGuess.equals("T")){
        correctSide = true;
    } else {
        correctSide = false;
    }
    updateBal(correctSide, getBet);
    return correctSide;
}
public static double updateBal(boolean correctSide, double getBet){
    double balance = getBal();
    if (correctSide == true){
        balance = getBet * 2 + balance;
        System.out.println("Correct. Your balance is now $" + balance);
    } else {
        balance = balance - getBet;
        System.out.println("Incorrect. Your balance is now $" + balance);
    }
    return balance;
}
public static boolean goAgain(){
    Scanner in = new Scanner(System.in);
    boolean validInput = false;
    String retryInput = null;
    while (validInput == false){
        System.out.println("Go again? (Y/N)");
        retryInput = in.next();
        if (retryInput.equals("Y") || retryInput.equals("N")){
            validInput = true;
        } else {
            JOptionPane.showInputDialog("Invalid Input: " + retryInput);
        }
    }
    if (retryInput.equals("Y")){
        return true;
    } else {
        System.out.println("You ended with: $" + getBal());
        return false;
    }
}
private static double balance;

public static double getBal() {
  return balance;
}
}

This is my code for a "Heads or Tails" game. My intention was to set balance to 100, then be changed each play. However, after each play, it resets to 100. How can I modify my code to make it 100 only on the first play?

Thanks.

Also: Any tips on things I'm doing oddly are appreciated.

The problem is with the updateBal method.

You have already declared a balance class variable, but you declared another balance variable local to that method. You successfully update the local balance , but not the class balance .

First, call your local copy something else; it's confusing to have two variable of the same name in scope at the same time. Then, at the end of the method, make sure to assign that value back to the class variable balance .

Change this line

updateBal(correctSide, getBet);

to

this.balance = updateBal(correctSide, getBet);

Why?

Because in your updateBal method you use this line

double balance = getBal();

Which COPY the value of class variable balance into the local variable balance . This local variable is DELETED when the updateBal method is over. If you have class variable and the local variable with the same name, default option is to work with local variable. You can force java to work with class variable by "this".

For example, you can change your method updateBal to this, so you dont have to return any value :

public static void updateBal(boolean correctSide, double getBet){
    if (correctSide == true){
        balance = getBet * 2 + balance;
        System.out.println("Correct. Your balance is now $" + balance);
    } else {
        balance = balance - getBet;
        System.out.println("Incorrect. Your balance is now $" + balance);
    }
}

beacuse there is no local variable called "balance" the class variable "balance" is chosen instead.


btw : The correct way how to solve this problem is by creating new class "Poker" and create instance of this class in the main method.

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