调用某些方法

时间:2013-10-09 00:18:18

标签: java methods

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

编辑了代码。

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

我该如何解决这个问题?

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

1 个答案:

答案 0 :(得分:4)

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

public static void main(String[] args) {

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

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

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

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

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

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

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

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

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