2013-10-09 73 views
0
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?變量重置

謝謝。

另外:我奇怪的事情的任何提示都表示讚賞。

回答

3

問題出在updateBal方法。

您已聲明balance類變量,但是您聲明另一個balance變量局部於該方法。您成功更新了本地balance,但未成功更新balance

首先,調用你的本地副本別的東西;在同一時間範圍內有兩個同名變量是令人困惑的。然後,在方法結束時,確保將該值分配回類變量balance

+0

謝謝,沒有抓住。 –

0

改變這一行

updateBal(correctSide, getBet); 

this.balance = updateBal(correctSide, getBet); 

爲什麼?

因爲你updateBal方法您使用此行

double balance = getBal(); 

哪個副本類變量balance的值到本地變量balance。當updateBal方法結束時,此局部變量將被刪除。如果您有類變量和具有相同名稱的本地變量,則默認選項是使用局部變量。 您可以通過「this」強制java使用類變量。

例如,你可以在你的方法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); 
    } 
} 

怎麼一回事,因爲沒有所謂的「平衡」類變量「平衡」局部變量選擇代替。


BTW:正確的做法應該如何解決這個問題是通過創建新類「撲克」,並在main方法創建該類的實例。