このゲームのプレイを作成しようとしています。ビルドしていますが、理解できないというエラーが表示されています。私が必要とするのは、プレーヤーを作成し、各プレーヤーのユーザー名とスコアポイントを表示することです。私が持っているコードが正しくないと信じているので、私が得ているエラーはプレーヤークラスにあります。私はこのプロジェクトが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);
}
}
}
実際にどのようなエラーが発生していますか? –
とは何ですか?あなたは例外を取得していますか?それはコンパイルされませんか?予想される出力と異なる出力ですか? – SomeJavaGuy
それはプレーヤーを作成しません –