java高手帮我看看这个程序错在哪里了 ,谢谢了阿

2024-11-03 04:27:23
推荐回答(2个)
回答1:

错误比较橡羡多,主要注意字母大小写,还有在一个文件里只能橡庆使有main方法的那个类是public的类,其它不能是public的,我帮你改好了我对对看呀:

class GuessGame {
Player p1;
Player p2;
Player p3;

public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of a number between 0 and 9....");
while (true)

{
System.out.println("Number to guess is " + targetNumber);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println("Player one guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Player two guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player three guessed " + guessp3);
if (guessp1 == targetNumber) {
p1isRight = true;
}
if (guessp2 == targetNumber) {
p2isRight = true;
}
if (guessp3 == targetNumber) {
p3isRight = true;
}
if (p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Play one got it right? " + p1isRight);
System.out.println("Play two got it right? " + p2isRight);
System.out.println("Play three got it right? " + p3isRight);
System.out.println("Game is over.");
break;
} else {
System.out.println("Players will have to try again."梁如拍);
}
}
}
}

class Player {
int number = 0;

public void guess() {
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}

public class GameLauncher {
public static void main(String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}

回答2:

改好了,用下面芹困碧的代码把你原来嫌举所有的代码覆盖就行了
class GuessGame {
Player p1;
Player p2;
Player p3;

public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of a number between 0 and 9....");
while(true)
{
System.out.println("Number to guess is "尺凳 + targetNumber);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println("Player one guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Player two guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player three guessed " + guessp3);
if (guessp1 == targetNumber) {
p1isRight = true;
}
if (guessp2 == targetNumber) {
p2isRight = true;
}
if (guessp3 == targetNumber) {
p3isRight = true;
}
if (p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Play one got it right? " + p1isRight);
System.out.println("Play two got it right? " + p2isRight);
System.out.println("Play three got it right? " + p3isRight);
System.out.println("Game is over.");
break;
} else {
System.out.println("Players will have to try again.");
}
}
}
}

class Player {
int number = 0;

public void guess() {
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}

public class GameLauncher {
public static void main(String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}