2012-04-26 73 views
0

這是一個我玩過猜猜遊戲的代碼,但問題在於我有幾個作爲java初學者不擅長的問題,需要一些指導。沿代碼有一些錯誤,我用側面的箭頭突出顯示。猜測遊戲java類的問題〜

import java.util.*; 

public class GuessingGame 
{ 


    private static Player house; 
    private static Player player; 

    private static int wins; 
    private static int loses; 
    private String name; 
    int card1,card2; 
    private int value; 



    public void Player(String name){ 

     this.name=name; 
     card1 = (Integer) null; 
     card2 = (Integer) null; 
    } 



public void Card(int value){ 

    this.value = value; 
    } 





public int getValue(){ 
      return value; 
     } 



public void acceptDeal(Card card1, Card card2){ 
     Random r = new Random(); 
     int value = r.nextInt(13) + 1; 
     card1 = new Card(value);   <<<<<<<<======= Error 1 
     value = r.nextInt(13) + 1; 
     card2 = new Card(value);   <<<<<<<<======= Error 2 
    } 



public static void init() 
{ 

    house = new Player("House");     <<<<<<<<======= Error 3 
    player = new Player("Player");    <<<<<<<<======= Error 4 
    wins = 0; 
    loses = 0; 

} 


    public static void playGame() 
    { 
     Scanner scan = new Scanner(System.in); 

     char option, playAgain; 
     int houseHandStrength, playerHandStrength; 
     System.out.println("Welcome to our card guess 1.0 game!"); 
     System.out.println(); 

     do { 
      // Deal cards to the house and player. 
      house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength)); <<<<<=== Error 5 
      player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength)); <<<<<=== Error 6  

      System.out.println(house); 

      // Determine whether the player wants to play this hand. 
      do { 
       System.out.print("Deal cards? (Y/N) "); 
       option = Character.toLowerCase(scan.next().charAt(0)); 
      } 
      while (option != 'n' && option != 'y'); 

      if (option == 'y') 
      { 
       System.out.println(player); 

       // Display hand strength of both players. 
       houseHandStrength = house.getHandStrength(); <<<<<=== Error 7 
       playerHandStrength = player.getHandStrength(); <<<<<=== Error 8 
       System.out.println("The dealer's hand strength is: " + houseHandStrength); 
       System.out.println("Your hand strength is: " + playerHandStrength); 
       System.out.println(); 

       // If the player has a stronger hand. 
       if (player.getHandStrength() > house.getHandStrength()) 
       { 
        System.out.println("** You won the hand! **"); 
        wins++; 
       } 
       else { 
        System.out.println("The house wins this round!"); 
        loses++; 
       } 
      } 

      // Display the win/lose statistics. 
      System.out.println("Current wins: " + wins); 
      System.out.println("Current loses: " + loses); 

      // Prompt whether the user wants to play again. 
      do { 
       System.out.print("Would you like to play again? (Y/N) "); 
       playAgain = Character.toLowerCase(scan.next().charAt(0)); 
      } 
      while (playAgain != 'n' && playAgain != 'y');   

      System.out.println(); 
      System.out.println("*******************************************************"); 
     } 
     while (playAgain == 'y'); 

     System.out.println(); 
     System.out.println("Thank you for playing!"); 
    } 

    public static void main(String[] args) 
    { 
     init(); 
     playGame(); 
    } 
} 
+0

你的班級Card and Player在哪裏;-)?另外,避免使用大寫字母書寫方法。 – 2012-04-26 11:53:31

+0

錯誤1 + 2您嘗試從方法創建對象?將你的Card(int value)方法重命名爲card(int value),還有一個你試圖實例化的Card類嗎? – 2012-04-26 11:56:11

+0

認爲其他類是[here](http://stackoverflow.com/q/10329526/799586) – 2012-04-26 12:00:50

回答

1

首先歡迎來到StackOverflow。很高興看到您找到並使用了作業標籤。請記住,要讓人們能夠幫助你,你需要提供更多信息。你所說的錯誤的意思是,當你運行該代碼等

關於你的錯誤,看來你還沒有真正定義的類CardPlayer,你有什麼有在你的代碼是兩種方法GuessingGame.Card()會發生什麼和GuessingGame.Player()在你的GuessingGame類。將它們改爲內(或外)類,它應該沒問題;)

1

也許你需要導入頂部的其他類?

這些問題似乎只存在於您自己的類中,程序輸出對錯誤有什麼看法?

public void Player(String name) ... 和 public void Card(int value) ...

應課吧?將它們聲明爲另一個文件中的類,並將它們包含到主文件中。

1

在您之前的Questioncard1card2類型爲Card。那是對的,現在你已經改變了這個,現在它是錯誤的。

1

你似乎已經把你的代碼捆綁起來了。你已經結合了玩家,卡牌和遊戲類。我沒有方便的Java編譯器,但你要做的是打破這三個模型。

錯誤1-6是在類別不存在時嘗試實例化新對象的結果。錯誤7-8是嘗試調用相同方法的結果。

import java.util.*; 

class Player { 
    int card1, card2; 
    private String name; 

    public void Player(String name){ 
     this.name=name; 
     card1 = (Integer) null; 
     card2 = (Integer) null; 
    } 

    public void acceptDeal(Card card1, Card card2){ 
     Random r = new Random(); 
     int value = r.nextInt(13) + 1; 
     card1 = new Card(value);   <<<<<<<<======= Error 1 
     value = r.nextInt(13) + 1; 
     card2 = new Card(value);   <<<<<<<<======= Error 2 
    } 
} 


class Card { 
    private int value; 

    public void Card(int value){ 
     this.value = value; 
    } 

    public int getValue(){ 
     return value; 
    } 
} 


public class GuessingGame 
{ 
    private static Player house; 
    private static Player player; 
    private static int wins; 
    private static int loses; 

    public static void init() 
    { 
     house = new Player("House");     <<<<<<<<======= Error 3 
     player = new Player("Player");    <<<<<<<<======= Error 4 
     wins = 0; 
     loses = 0; 
    } 

    public static void playGame() 
    { 
     Scanner scan = new Scanner(System.in); 

     char option, playAgain; 
     int houseHandStrength, playerHandStrength; 
     System.out.println("Welcome to our card guess 1.0 game!"); 
     System.out.println(); 

     do { 
      // Deal cards to the house and player. 
      house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength)); <<<<<=== Error 5 
      player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength)); <<<<<=== Error 6  

      System.out.println(house); 

      // Determine whether the player wants to play this hand. 
      do { 
       System.out.print("Deal cards? (Y/N) "); 
       option = Character.toLowerCase(scan.next().charAt(0)); 
      } 
      while (option != 'n' && option != 'y'); 

      if (option == 'y') 
      { 
       System.out.println(player); 

       // Display hand strength of both players. 
       houseHandStrength = house.getHandStrength(); <<<<<=== Error 7 
       playerHandStrength = player.getHandStrength(); <<<<<=== Error 8 
       System.out.println("The dealer's hand strength is: " + houseHandStrength); 
       System.out.println("Your hand strength is: " + playerHandStrength); 
       System.out.println(); 

       // If the player has a stronger hand. 
       if (player.getHandStrength() > house.getHandStrength()) 
       { 
        System.out.println("** You won the hand! **"); 
        wins++; 
       } 
       else { 
        System.out.println("The house wins this round!"); 
        loses++; 
       } 
      } 

      // Display the win/lose statistics. 
      System.out.println("Current wins: " + wins); 
      System.out.println("Current loses: " + loses); 

      // Prompt whether the user wants to play again. 
      do { 
       System.out.print("Would you like to play again? (Y/N) "); 
       playAgain = Character.toLowerCase(scan.next().charAt(0)); 
      } 
      while (playAgain != 'n' && playAgain != 'y');   

      System.out.println(); 
      System.out.println("*******************************************************"); 
     } 
     while (playAgain == 'y'); 

     System.out.println(); 
     System.out.println("Thank you for playing!"); 
    } 

    public static void main(String[] args) 
    { 
     init(); 
     playGame(); 
    } 
}