尝试为我正在构建的这个游戏创建一个游戏,但它给了我一个我不理解的错误。我需要它做的是创建一个玩家并显示每个玩家的用户名和得分点。我得到的错误是在播放器类中,因为我相信我的代码是不正确的。我需要帮助,请在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)
我更正了代码以解决编译错误,但您应该提供类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
}
}
}