2012-04-26 11 views
0

これは私が作った推測ゲームをするためのコードですが、問題は、私の初心者としては初心者ではありませんし、いくつかの指導が必要なことです。コードに沿っていくつかのエラーがありました。私は側面の矢印で強調しました。ゲームの推測に伴う問題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

クラスカードとプレーヤーはどこですか?-)?また、大文字でメソッドを書くのは避けてください。 –

+0

エラー1 + 2メソッドからオブジェクトを作成しようとしていますか? Card(int value)メソッドの名前をcard(int value)に変更します。インスタンス化しようとしているCardクラスもありますか? –

+0

他のクラスは[ここ]と思う(http://stackoverflow.com/q/10329526/799586) –

答えて

1

まずは、StackOverflowへようこそ。あなたが宿題を見つけて使用していることを確認することは素晴らしいことです。人々があなたを助けることができるためには、より多くの情報を提供する必要があることに留意してください。あなたがエラーとはどういう意味ですか、何があなたがあなたが得るエラーに関するコードなど

を実行すると、あなたがそこにあなたのコード内で持っている二つの方法GuessingGame.Card()が何であるか、あなたは本当にクラスCardPlayerを定義していないと表示されますが起こりますGuessingGameクラスのGuessingGame.Player()それらを内部(または外部)クラスに変更してください。

1

トップに他のクラスをインポートする必要がありますか?

問題は自分のクラスでしかないように見えますが、プログラムの出力からはエラーの内容はどうなりますか?

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

は、クラス、右すべきですか?それらを別のファイルのクラスとして宣言し、それらをメインファイルに含めます。

1

以前のQuestioncard1およびcard2では、Cardでした。それは正しかった、今あなたはこれを変更して、今それは間違っています。

1

あなたのコードを束ねたようです。あなたは、プレイヤー、カード、ゲームのクラスを組み合わせました。私は便利なJavaコンパイラを持っていませんが、あなたがしようとしているのは、3つのモデルを打ち破ることです。

エラー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(); 
    } 
} 
関連する問題