• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The Code is not compiling,and also tell me what will be the filename for this code

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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("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;
}
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();
}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it doesn't compile, there must have been an error message, right? What was it? (What the file name will be you'll see as soon as it does compile :-)

In the future, if you post code of any length, surround it with CODE tags, which you can do by clicking the "CODE" button underneath the text entry area. That way, the code will be displayed formatted.
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Change the following in your code
and your class name is


compile and run

bye for now
sat
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!

There can be only one public class so just make the class GameLaucher as public and remove the public keyword from the other 2 classes. Also remember that naming convention is to capitalise the class names. It is a good and the right way of coding.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... Another thing that you should get used to is putting each (top-level) class in its own file. Making all but one of your classes non-public is a hack. In the long run, whether or not a class is public is a design decision but the reason should not be because one was too lazy to create separate files.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic