简体   繁体   English

变量重置

[英]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. 这是我的“ Heads or Tails”游戏代码。 My intention was to set balance to 100, then be changed each play. 我的意图是将平衡设置为100,然后每次播放更改一次。 However, after each play, it resets to 100. How can I modify my code to make it 100 only on the first play? 但是,每次播放后,它都会重置为100。如何修改我的代码,使其仅在第一次播放时才变为100?

Thanks. 谢谢。

Also: Any tips on things I'm doing oddly are appreciated. 另外:任何关于我正在做的事情的提示都值得赞赏。

The problem is with the updateBal method. 问题在于updateBal方法。

You have already declared a balance class variable, but you declared another balance variable local to that method. 您已经声明了一个balance类变量,但是您声明了该方法本地的另一个balance变量。 You successfully update the local balance , but not the class balance . 您成功更新了本地balance ,而不是类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 . 然后,在方法结束时,请确保将该值分配回类变量balance

Change this line 更改此行

updateBal(correctSide, getBet);

to

this.balance = updateBal(correctSide, getBet);

Why? 为什么?

Because in your updateBal method you use this line 因为在您的updateBal方法中使用了这一行

double balance = getBal();

Which COPY the value of class variable balance into the local variable balance . 其中将类变量balance COPY值转换为局部变量balance This local variable is DELETED when the updateBal method is over. updateBal方法结束后,将删除此局部变量。 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". 您可以通过“ this”强制Java使用类变量。

For example, you can change your method updateBal to this, so you dont have to return any value : 例如,您可以将方法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);
    }
}

beacuse there is no local variable called "balance" the class variable "balance" is chosen instead. 因为没有名为“ balance”的局部变量,所以选择了“ balance”类变量。


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. btw:解决此问题的正确方法是通过创建新类“ Poker”并在main方法中创建此类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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