提取发送给我的代码,我收到2个错误,两个"类已定义"
错误在Player() {
和
Multiplayer () {
我尝试用多种不同方式制作它,并且完全无法让它运行。如果有人有任何建议,请提前告知我们。
代码如下:
import java.util.*;
public class Player {
int chances[];
boolean gameover;
int noofchances;
Player() {
chances = new int[100];
gameover = false;
noofchances = 0;
}
void playgame() {
Random r = new Random();
Scanner sc = new Scanner(System.in);
int p;
int i = 0;
int guess;
p = r.nextInt(100);
while (i < 5) // use here while(guess == false)
{
System.out.println("Guess a number:"); // I used this System.out.println("Guess a number:"+p);
guess = sc.nextInt();
if (guess > p)
System.out.println("Too High.");
else if (guess < p)
System.out.println("Too Low.");
else if (guess == p) {
System.out.println("CORRECT!");
gameover = true;
break;
}
{
chances[i] = guess;
i++;
}
noofchances = i;
}
}
void print() {
int i;
System.out.println("Number of responses:" + noofchances);
for (i = 0; i < noofchances; i++)
System.out.print(chances[i] + " ");
}
}
class MultiGame {
public static void main(String args[]) {
int noofplayers = 0;
Scanner s = new Scanner(System.in); // creating object to read input from keyboard
int i;
int playagain = 1;
System.out.println("Enter number of users:");
noofplayers = s.nextInt();
Player ps[] = new Player[noofplayers]; // declaring array of player objects.
while (playagain == 1) {
for (i = 0; i < noofplayers; i++) {
ps[i] = new Player(); // allocating memory for the player.
ps[i].gameover = false;
ps[i].playgame();
}
for (i = 0; i < noofplayers; i++) {
System.out.println("Player " + i + 1);
ps[i].print();
}
System.out.println("Do wish to play again. Press 1 to continue.... ");
playagain = s.nextInt();
}
}
}