1
import java.util.Scanner;

import javax.swing.JOptionPane;

public class HW {

public static void main(String[] args){
    while (retry == true){
        getGuess();
        getBet(balance);
        checkGuess(getGuess());
        updateBal(guessResult, betParsed);
        goAgain(balance);
    }
}


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

public static boolean checkGuess(String sideGuess){
    //Checks if the side is correct
    double num = Math.round(Math.random());
    boolean guessResult;
    if (num == 0 && sideGuess.equals("H")){
        System.out.println("Correct. The side was: H");
        guessResult = true;
        return true;
    } else if (num == 1 && sideGuess.equals("T")){
        System.out.println("Correct. The side was: T");
        guessResult = true;
        return true;
    } else {
        System.out.println("Incorrect.");
        guessResult = false;
        return false;
    }
}

public static double getBet(double balance){
    //Retrieves a bet from the user
    Scanner in = new Scanner(System.in);
    String betInput = null;
    double betParsed = 0;
    boolean validInput = false;
    while (validInput == false){
        betInput = in.next();
        try {
            betParsed = Double.parseDouble(betInput);
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invlaid Input: " + betInput);
        }
        if (betParsed > balance || betParsed < 0){
            JOptionPane.showMessageDialog(null, "Invalid Input! You have: $" + balance);
        } else {
            validInput = true;
        }
    }
    return betParsed;
}

public static double updateBal(boolean checkGuess, double getBet){
    //Updates the balance based on the bet
    double balance = 0;
    if (checkGuess == true){
        balance = balance + getBet * 2;
        System.out.println("Your balance is now: $" + balance);
    } else {
        System.out.println("Your balance is now: $" + balance);
        balance = balance - getBet;
    }
    return balance;
}

public static boolean goAgain(double balance){
    //Determines if new play is needed
    Scanner in = new Scanner(System.in);
    boolean validInput = false;
    String goAgainInput = null;
    boolean retry;
    while (validInput == false){
        System.out.println("Go again? (Y/N)");
        goAgainInput = in.next();
        if (goAgainInput.equals("Y") || goAgainInput.equals("N")){
            validInput = true;          
        } else {
            JOptionPane.showMessageDialog(null, "Invalid Input: " + goAgainInput);
        }
    }
    if (goAgainInput.equals("Y")){
        retry = true;
        return true;
    } else {
        System.out.println("You ended with: $" + balance);
        retry = false;
        return false;
    }
}
}

编辑了代码。

我试图将一些定义的变量传递给一些方法,但似乎我不能使用它们?

我怎样才能解决这个问题?

似乎它们不是“本地的”,我无法理解,因为它们是在方法中定义的。我一定是在想。

4

1 回答 1

4

main()方法应如下所示,只有一个参数:

public static void main(String[] args) {

对于第二个问题,updateBal()接收两个参数,但您没有传递任何参数。所以编译器正确地抱怨你应该根据你想要做的事情传递它们:

updateBal(false, 0); // pass the right values

关于将参数传递给方法,这是错误的:

getBet(balance);
checkGuess(getGuess());
updateBal(guessResult, betParsed);

您会看到,变量guessResultbetParsed是定义它们的方法的本地变量,您不能在外部使用它们。并且这些方法返回一个值,该值会丢失,因为您没有使用或存储它。这两个问题都有一个简单的解决方案 - 声明方法本地的新变量:

double betParsed = getBet(balance);
boolean guessResult = checkGuess(getGuess());
updateBal(guessResult, betParsed);

同样,同样的问题在这里,在主循环中:

boolean retry = true;
while (retry) {
    // ...
    retry = goAgain(balance);
}

底线:您必须对方法返回的值做一些事情,并且在方法内声明的变量在它们之外将可见。

于 2013-10-09T00:21:28.963 回答