Java Newbie..need help in guessing game program
Hi. I'm starting to learn java since a few days..but found myself staring at this roadblock soon enough..the program is as following..i'm not able to compile it because it says error,the file name should be the same as the class name..
Can somebody please correct the errors in the following program and also suggest the correct file name for the same..?
Code Java:
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.printn("Player one guessed " + guessp1);
guessp2 = p2.number;
System.out.printn("Player two guessed " + guessp2);
guessp3 = p3.number;
System.out.printn("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("Player one got it right? " +p1isRight);
System.out.println("Player two got it right? " +p2isRight);
System.out.println("Player three got it right? " +p3isRight);
System.out.println("Game's up.");
break;
}
else System.out.println("Players will have to try again.");
}
}
}
public 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();
}
}
Re: Java Newbie..need help in guessing game program
First off, we do not know anything about your program or what it does. We aren't going to debug your program for you.. BUT we can guide you.
Saying: "Hey my program has a bunch of errors, go find them and show me exactly how to do it" ... doesn't really seem enticing for most.
So tell us what errors you're having, what you've tried, and what you're thinking about doing to fix them.
Also, for the class name.. It must match your .java file EXACTLY. Spacing, spelling, capitalization, etc... must all be the same.
Bad Example: filename.java -> class File name THIS WON'T WORK. The f is capitalized and there is a space between the two words. Don't do this.
However..
Good Example: filename.java -> class filename This WILL work. Everything matches exactly...
Please comment back on what your issues are. It makes it easier for us, and helps you learn more. You won't learn by having the answers thrown at you every time you need it.
Re: Java Newbie..need help in guessing game program
Re: Java Newbie..need help in guessing game program
First off,my apologies for the mis-communication.
So the specific problem I'm facing with the above program is this..
As you can see,there are 2 public classes..'Player' and 'GameLauncher'. But the game launcher is the class which contains the main method..so now,even though I name the file exactly as that <classname>.java,it still does not compile. Any suggestions?
I have tried naming the file as 'Player' and 'GameLauncher' both,with exact resemblance..but still without luck.
Re: Java Newbie..need help in guessing game program
Oh yeah,it gives me that error which says the file name should be same as the public class name's..but since there are 2 public classes..I'm stuck.
Re: Java Newbie..need help in guessing game program
Split up the classes into 2 separate files named that of the class they contain, or make the class access modifiers package private (in other words, remove the public modifier - not recommended).
Re: Java Newbie..need help in guessing game program
Here is how it would go,
Name this, GuessGame.java
Code :
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.printn("Player one guessed " + guessp1);
guessp2 = p2.number;
System.out.printn("Player two guessed " + guessp2);
guessp3 = p3.number;
System.out.printn("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("Player one got it right? " +p1isRight);
System.out.println("Player two got it right? " +p2isRight);
System.out.println("Player three got it right? " +p3isRight);
System.out.println("Game's up.");
break;
}
else System.out.println("Players will have to try again.");
}
}
}
Name this, Player.java
Code :
public class Player {
int number = 0;
public void guess() {
number = (int) (Math.random()*10);
System.out.println("I'm guessing " +number);
}
}
Name this GameLauncher.java
Code :
public class GameLauncher {
public static void main (String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
You can't have more than one class in the same file, it won't work, another suggestion, get and IDE, such as eclipse or netbeans, it helps for people with all skill levels in java, especially those new to java.
Re: Java Newbie..need help in guessing game program
Finally ot my code working!! Thank you so much! Thank you all..
Final solution that got it working..
Split the file into 3 files according to the classes, renamed them to their respective public classes, voila!works..
@tyb97..i'm actually referring to the book head first java, and they ask to use notepad b4 using netbeans..so trying their formula out.. :) thank you.