变量重置 import java.util.Scanner; 导入javax.swing.JOptionPane; 公共类硬件{ 公共静态void main(字符串[]args){ 余额=100; 布尔增益=真; while(goAgain==true){ checkGuess(getGuess(),getBet(balance)); goAgain=goAgain(); } } 公共静态字符串getGuess(){ 扫描仪输入=新扫描仪(系统输入); 字符串猜测=null; 布尔值validInput=false; while(validInput==false){ System.out.println(“猜测:(H/T)”); 猜测=in.next(); 如果(猜测等于(“H”)|猜测等于(“T”)){ validInput=true; }否则{ showMessageDialog(null,“无效输入:”+猜测); } } 返回猜测; } 公共静态双getBet(双余额){ 扫描仪输入=新扫描仪(系统输入); 字符串输入=null; 双betParsed=0; 布尔值validInput=false; while(validInput==false){ System.out.println(“打赌?您有:$”+余额); betInput=in.next(); 试一试{ betParsed=Double.parseDouble(betInput); }捕获(数字格式){ showMessageDialog(null,“无效输入:”+betInput); } 如果(betParsed>balance | | betParsed

变量重置 import java.util.Scanner; 导入javax.swing.JOptionPane; 公共类硬件{ 公共静态void main(字符串[]args){ 余额=100; 布尔增益=真; while(goAgain==true){ checkGuess(getGuess(),getBet(balance)); goAgain=goAgain(); } } 公共静态字符串getGuess(){ 扫描仪输入=新扫描仪(系统输入); 字符串猜测=null; 布尔值validInput=false; while(validInput==false){ System.out.println(“猜测:(H/T)”); 猜测=in.next(); 如果(猜测等于(“H”)|猜测等于(“T”)){ validInput=true; }否则{ showMessageDialog(null,“无效输入:”+猜测); } } 返回猜测; } 公共静态双getBet(双余额){ 扫描仪输入=新扫描仪(系统输入); 字符串输入=null; 双betParsed=0; 布尔值validInput=false; while(validInput==false){ System.out.println(“打赌?您有:$”+余额); betInput=in.next(); 试一试{ betParsed=Double.parseDouble(betInput); }捕获(数字格式){ showMessageDialog(null,“无效输入:”+betInput); } 如果(betParsed>balance | | betParsed,java,variables,methods,Java,Variables,Methods,,问题在于updateBal方法 您已经声明了一个余额类变量,但是您声明了另一个余额变量作为该方法的本地变量。您成功地更新了本地余额,但没有更新类余额 首先,调用本地副本;同时在作用域中有两个同名的变量是令人困惑的。然后,在方法末尾,确保将该值赋回类变量balance问题在于updateBal方法 您已经声明了一个余额类变量,但是您声明了另一个余额变量作为该方法的本地变量。您成功地更新了本地余额,但没有更新类余额 首先,调用本地副本;同时在作用域中有两个同名的变量是令人困惑的。然后,在方法末尾,

,问题在于
updateBal
方法

您已经声明了一个
余额
类变量,但是您声明了另一个
余额
变量作为该方法的本地变量。您成功地更新了本地
余额
,但没有更新类
余额


首先,调用本地副本;同时在作用域中有两个同名的变量是令人困惑的。然后,在方法末尾,确保将该值赋回类变量
balance

问题在于
updateBal
方法

您已经声明了一个
余额
类变量,但是您声明了另一个
余额
变量作为该方法的本地变量。您成功地更新了本地
余额
,但没有更新类
余额


首先,调用本地副本;同时在作用域中有两个同名的变量是令人困惑的。然后,在方法末尾,确保将该值赋回类变量
balance

问题在于
updateBal
方法

您已经声明了一个
余额
类变量,但是您声明了另一个
余额
变量作为该方法的本地变量。您成功地更新了本地
余额
,但没有更新类
余额


首先,调用本地副本;同时在作用域中有两个同名的变量是令人困惑的。然后,在方法末尾,确保将该值赋回类变量
balance

问题在于
updateBal
方法

您已经声明了一个
余额
类变量,但是您声明了另一个
余额
变量作为该方法的本地变量。您成功地更新了本地
余额
,但没有更新类
余额

首先,调用您的本地副本;同时在作用域中有两个同名的变量是令人困惑的。然后,在方法末尾,确保将该值赋回类变量
balance

更改此行

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.balance = updateBal(correctSide, getBet);

为什么?

因为在您的
updateBal
方法中,您使用这一行

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.balance = updateBal(correctSide, getBet);
它将类变量
balance
的值复制到局部变量
balance
中。当
updateBal
方法结束时,此局部变量将被删除。如果类变量和局部变量具有相同的名称,默认选项是使用局部变量。 您可以通过“this”强制java使用类变量

例如,您可以将方法
updateBal
更改为此,这样就不必返回任何值:

double balance = getBal();
因为没有称为“balance”的局部变量,所以选择了类变量“balance”


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

更改这一行

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.balance = updateBal(correctSide, getBet);

为什么?

因为在您的
updateBal
方法中,您使用这一行

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.balance = updateBal(correctSide, getBet);
它将类变量
balance
的值复制到局部变量
balance
中。当
updateBal
方法结束时,此局部变量将被删除。如果类变量和局部变量具有相同的名称,默认选项是使用局部变量。 您可以通过“this”强制java使用类变量

例如,您可以将方法
updateBal
更改为此,这样就不必返回任何值:

double balance = getBal();
因为没有称为“balance”的局部变量,所以选择了类变量“balance”


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

更改这一行

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.balance = updateBal(correctSide, getBet);

为什么?

因为在您的
updateBal
方法中,您使用这一行

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.balance = updateBal(correctSide, getBet);
它将类变量
balance
的值复制到局部变量
balance
中。当
updateBal
方法结束时,此局部变量将被删除。如果类变量和局部变量具有相同的名称,默认选项是使用局部变量。 您可以通过“this”强制java使用类变量

例如,您可以将方法
updateBal
更改为此,这样就不必返回任何值:

double balance = getBal();
因为没有称为“balance”的局部变量,所以选择了类变量“balance”


顺便说一句:解决这个问题的正确方法是创建新的类“扑克”