变量重置

时间:2013-10-09 22:26:57

标签: java variables methods

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;
}
}

这是我的“头或尾”游戏的代码。 我的意图是将余额设置为100,然后每次更改。 但是,每次比赛后,重置为100。 如何在第一次播放时修改我的代码才能使其成为100?

感谢。

另外:对于我正在做的奇怪事情的任何提示都表示赞赏。

2 个答案:

答案 0 :(得分:3)

问题在于updateBal方法。

您已经声明了一个balance类变量,但是您为该方法声明了另一个balance变量。您已成功更新本地balance,但未成功更新课程balance

首先,将本地副本称为其他内容;在同一时间在范围内有两个同名变量令人困惑。然后,在方法结束时,确保将该值赋回给类变量balance

答案 1 :(得分:0)

更改此行

updateBal(correctSide, getBet);

this.balance = updateBal(correctSide, getBet);

为什么?

因为在updateBal方法中使用此行

double balance = getBal();

将类变量balance的值复制到局部变量balance中。 updateBal方法结束时,此局部变量为DELETED。如果您有类变量和具有相同名称的局部变量,则默认选项是使用局部变量。 您可以通过“this”强制java使用类变量。

例如,您可以将方法updateBal更改为此方法,因此您无需返回任何值:

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);
    }
}

因为没有名为“balance”的局部变量,所以选择了类变量“balance”。


顺便问一下:如何解决这个问题的正确方法是创建新类“Poker”并在main方法中创建此类的实例。