• Beginner help
    1 replies, posted
I have recently gotten the Head First java book and I am trying to get this Guessing game program to work. I am using netbeans and after I type in the code displayed in the book I get an error: Main method not found in class guessgame.GuessGame, please define the main method as: public static void main(String[] args) I think it has something to do with the classes at the bottom. Here is the program: 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 guess " + guessp1); guessp2 = p2.number; System.out.println("Player two guess " + guessp2); guessp3 = p3.number; System.out.println("Player three guess " + 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 winnter!"); 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 is over."); break; //game over, so break out of the loop } else { //we must keep going because nobody got it right! System.out.println("Players will have to try again."); } } } } public class Player { int number = 0; //where guess goes 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(); } } Please help a newb!
[QUOTE=SStubbs84;41836947]I have recently gotten the Head First java book and I am trying to get this Guessing game program to work. I am using netbeans and after I type in the code displayed in the book I get an error: Main method not found in class guessgame.GuessGame, please define the main method as: public static void main(String[] args) I think it has something to do with the classes at the bottom. Here is the program: [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.println("Player one guess " + guessp1); guessp2 = p2.number; System.out.println("Player two guess " + guessp2); guessp3 = p3.number; System.out.println("Player three guess " + 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 winnter!"); 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 is over."); break; //game over, so break out of the loop } else { //we must keep going because nobody got it right! System.out.println("Players will have to try again."); } } } } public class Player { int number = 0; //where guess goes 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(); } } [/code] Please help a newb![/QUOTE] Added code tags for you. Anyway, it's been pretty long since I last used Java, but I think it's because the code is in a file named "GuessGame.java". This means that it will look in the class with the same name for the main method. Try moving the main method to inside your GuessGame class instead.
Sorry, you need to Log In to post a reply to this thread.