2017-04-19 8 views
-3

このゲームのプレイを作成しようとしています。ビルドしていますが、理解できないというエラーが表示されています。私が必要とするのは、プレーヤーを作成し、各プレーヤーのユーザー名とスコアポイントを表示することです。私が持っているコードが正しくないと信じているので、私が得ているエラーはプレーヤークラスにあります。私はこのプロジェクトが2時間以内に予定されており、私はたくさん苦労しています。Java - 2人のプレイヤーゲームを作成する

CAI.java

package cai; 

import java.util.Scanner; 
import java.util.Random; 
public class CAI { 
    private int correct; 

    public CAI() { 
     resetCorrect(0); 
    } 
    private String Username; 
    public int getCorrect(){ 
     return correct; 
    } 
    void Addition(){ 
     for(int count = 1; count <= 10; count++){ 
      System.out.println("Problem " + count + " of 10:"); 
      correct += this.doAddition(); 
     } 
    } 

    public int doAddition(){ 
     int count = 1; 
     Scanner scan = new Scanner(System.in); 
     Random g = new Random(); 
     int first = g.nextInt(10); 
     int second = g.nextInt(10); 
     int answer = first + second; 
     String toString = first + " + " + second + " = " + answer; 

     while (count <= 3){ 

      count++; 

      System.out.print(first + " + " + second + " = "); 

      int userAnswer = scan.nextInt(); 

      if (userAnswer == answer){ 

       this.printGoodComment(); 

       count = 10; //used as a flag value 

      }else{ 
       if (count != 4){ 
        this.printBadComment(); 
       } 
      } 
     } 
     if (count == 10){ 
      return 1; 
     }else{ 
      this.printFailure(toString); 
      return 0; 
     } 
    } 

    void Substraction(){ 
     for(int count = 1; count <= 10; count++){ 
      System.out.println("Problem " + count + " of 10:"); 
      correct += this.doSubstract(); 
     } 
    } 

    public int doSubstract(){ 
     int count = 1; 
     Scanner scan = new Scanner(System.in); 
     Random g = new Random(); 
     int first = g.nextInt(10); 
     int second = g.nextInt(10); 
     int answer = first - second; 
     String toString = first + " - " + second + " = " + answer; 
     while (count <= 3){ 
      count++; 
      System.out.print(first + " - " + second + " = "); 
      int userAnswer = scan.nextInt(); 
      if (userAnswer == answer){ 
       this.printGoodComment(); 
       count = 10; //used as a flag value 
      }else{ 
       if (count != 4){ 
        this.printBadComment(); 
       } 
      } 
     } 
     if (count == 10){ 
      return 1; 
     }else{ 
      this.printFailure(toString); 
      return 0; 
     } 
    } 

    void multplication(){ 
     for(int count = 1; count <= 10; count++){ 
      System.out.println("Problem " + count + " of 10:"); 
      correct += this.doMultiply(); 
     } 
    } 

    public int doMultiply(){ 
     int count = 1; 
     Scanner scan = new Scanner(System.in); 
     Random g = new Random(); 
     int first = g.nextInt(10); 
     int second = g.nextInt(10); 
     int answer = first * second; 
     String toString = first + " * " + second + " = " + answer; 
     while (count <= 3){ 
      count++; 

      System.out.print(first + " * " + second + " = "); 
      int userAnswer = scan.nextInt(); 
      if (userAnswer == answer){ 
       this.printGoodComment(); 
       count = 10; //used as a flag value 
      }else{ 
       if (count != 4){ 
        this.printBadComment(); 
       } 
      } 
     } 
     if (count == 10){ 
      return 1; 
     }else{ 
      this.printFailure(toString); 
      return 0; 
     } 
    } 
    void Division(){ 
     for(int count = 1; count <= 10; count++){ 
      System.out.println("Problem " + count + " of 10:"); 
      correct += this.doDivide(); 
     } 
    } 

    public int doDivide(){ 
     int count = 1; 
     Scanner scan = new Scanner(System.in); 
     Random g = new Random(); 
     int first = g.nextInt(10); 
     int second = g.nextInt(10); 
     int answer = first/second; 
     String toString = first + "/" + second + " = " + answer; 
     while (count <= 3){ 
      count++; 
      System.out.print(first + "/" + second + " = "); 
      int userAnswer = scan.nextInt(); 
      if (userAnswer == answer){ 
       this.printGoodComment(); 
       count = 10; //used as a flag value 
      }else{ 
       if (count != 4){ 
        this.printBadComment(); 
       } 
      } 
     } 
     if (count == 10){ 
      return 1; 
     }else{ 
      this.printFailure(toString); 
      return 0; 
     } 
    } 



    public void printFailure(String answer){ 
     System.out.println("Incorrect Solution"); 
     System.out.println("Solution is: " + answer); 
    } 

    public void resetCorrect(int n) { 
     correct = n; 
    } 

    /* Print math drill menu. */ 
    public void printMenu() { 
     System.out.println("Welcome to my CAI"); 
     System.out.println("1. Addition"); 
     System.out.println("2. Substraction"); 
     System.out.println("3. Multiplication"); 
     System.out.println("4. Division"); 
     System.out.println("\nPlease select the operation , or Q to quit: "); 
    } 

    public void printGoodComment(){ 
     Random g = new Random(); 
     int n = g.nextInt(4)+1; // n is between 1 - 4 
     switch (n){ 
     case 1: 
      System.out.println("Very good!"); 
      break; 
     case 2: 
      System.out.println("Excellent!"); 
      break; 
     case 3: 
      System.out.println("Nice Work!"); 
      break; 
     case 4: 
      System.out.println("Keep up the Good Work!"); 
      break; 
     } 

    } 
     public void printBadComment(){ 
      Random g = new Random(); 
      int n = g.nextInt(4)+1; // n is between 1 - 4 
      switch (n){ 

      case 1: 
       System.out.println("No. Please try again."); 
       break; 
      case 2: 
       System.out.println("Wrong. Try once more."); 
       break; 
      case 3: 
       System.out.println("Don't give up"); 
       break; 
      case 4: 
       System.out.println("No. Keep trying"); 
       break; 
     } 


    } 

} 

player.java

package cai; 

import java.util.Scanner; 
public class Player { 

    public static void main(String[] args) { 
      Player[] players = new Player[10]; 

    int totalPlayers = 0; 
    Player player1 = new Player("Yohana"); 
    players[0] = player1; 
    totalPlayers++; 

    Player player2 = new Player("Nick"); 
    players[1] = player2; 
    totalPlayers++; 



    System.out.println("Player 1: " + players[0]); 
    System.out.println("Player 2: " + players[1]); 

     int c; 
     CAI m = new CAI(); 
     System.out.println("This program was written by my name.\n"); 
     m.printMenu(); 
     Scanner in=new Scanner(System.in); 
     String choice=in.next(); 

     while (!choice.equalsIgnoreCase("Q")){ 
    if (choice.equals("Addition")){ 
      m.Addition(); 
    }else if (choice.equals("Substraction")){ 
      m.Substraction(); 
    }else if (choice.equals("Multiplication")){ 
      m.multplication(); 
    }else if (choice.equals("Division")){ 
      m.Division(); 

    } 



     System.out.println("You got " + m.getCorrect() + " right. " + m.getCorrect() + "0%"); 

     if ((double)m.getCorrect()/10 < .75){ 
      System.out.println("Please ask your instuctor for extra help."); 
     } 

      m.resetCorrect(0); 
      m.printMenu(); 
      choice=in.next(); 
     } 
    } 

    private Player (String name) 
    { 
     String Username = name; 
    } 
    //used for printing 
    public String toString() 
    { 
    return String.format("$s:", Username); 
    } 
    } 
} 
+0

実際にどのようなエラーが発生していますか? –

+0

とは何ですか?あなたは例外を取得していますか?それはコンパイルされませんか?予想される出力と異なる出力ですか? – SomeJavaGuy

+0

それはプレーヤーを作成しません –

答えて

0

私はコンパイルエラーを解決するためにコードを修正していますが実装を提供する必要がありますクラスCAIのメソッドの

import java.util.Scanner; 

public class Player { 

    private String username; 

    public static void main(String[] args) { 
     Player player = new Player("Bowls"); 
     CAI m = player.new CAI(); 
     Player[] players = new Player[10]; 

     int totalPlayers = 0; 
     Player player1 = new Player("Yohana"); 
     players[0] = player1; 
     totalPlayers++; 

     Player player2 = new Player("Nick"); 
     players[1] = player2; 
     totalPlayers++; 

     System.out.println("Player 1: " + players[0]); 
     System.out.println("Player 2: " + players[1]); 

     int c; 
     System.out.println("This program was written by my name.\n"); 
     m.printMenu(); 
     Scanner in = new Scanner(System.in); 
     String choice = in.next(); 

     while (!choice.equalsIgnoreCase("Q")) { 
      if (choice.equals("Addition")) { 
       m.Addition(); 
      } else if (choice.equals("Substraction")) { 
       m.Substraction(); 
      } else if (choice.equals("Multiplication")) { 
       m.multplication(); 
      } else if (choice.equals("Division")) { 
       m.Division(); 

      } 

      System.out.println("You got " + m.getCorrect() + " right. " + m.getCorrect() + "0%"); 

      if ((double) m.getCorrect()/10 < .75) { 
       System.out.println("Please ask your instuctor for extra help."); 
      } 

      m.resetCorrect(0); 
      m.printMenu(); 
      choice = in.next(); 
     } 
    } 

    private Player(String name) { 
     username = name; 
    } 

    // used for printing 
    public String toString() { 
     return String.format("$s:", username); 
    } 

    class CAI { 

     public CAI() { 
      // TODO Auto-generated constructor stub 
     } 

     public void printMenu() { 
      // TODO Auto-generated method stub 

     } 

     public void resetCorrect(int i) { 
      // TODO Auto-generated method stub 

     } 

     public double getCorrect() { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     public void Division() { 
      // TODO Auto-generated method stub 

     } 

     public void multplication() { 
      // TODO Auto-generated method stub 

     } 

     public void Substraction() { 
      // TODO Auto-generated method stub 

     } 

     public void Addition() { 
      // TODO Auto-generated method stub 

     } 

    } 
} 
+0

In 'return String.format(" $ s: "、username);'、 '$'は '%'にする必要があります。 – jrook

関連する問題