java - 变量重置

标签 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?

谢谢。

另外:任何关于我正在做的奇怪事情的提示都会受到赞赏。

最佳答案

问题出在 updateBal 方法上。

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

首先,将您的本地副本命名为其他名称;同时在作用域中存在两个同名变量会令人困惑。然后,在方法结束时,确保将该值分配回类变量 balance

关于java - 变量重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283912/

相关文章:

java - 使用 ValidationMessages.properties 进行 Java bean 验证的国际化

c++ - 尝试创建一个更好的循环来命名数组元素

java - 从 Java 向 Clojure 传递参数

javascript - 是否可以使用 Jade 将 'a variable' 渲染为 'a-variable'?

python - python中是否可以调用带有变量函数的模块?

.net - 非泛型声明中不允许有约束

javascript - 是否可以覆盖某些 Javascript 命令?

java - 有没有办法判断 JUnit 测试是否正在从 Ant 运行?

java - 尝试创建一个带有 2D vector 和字符串数组头的 JTable,我该怎么做?

java - 无法连接到数据库网络错误 IOException : Connection refused: connect