(Bo Master original)
First of all, Bo Master is a freshman year end of the winter vacation when I read the Java, and then I look at Head first Java Chinese version, because a freshman learned C, so some of the basic ideas are still understood, Look at this book is a glance (that is, the kind of light to see no self-tapping code), and then see the book of a guess the number of games, you want to manually tap, familiar with the Java syntax, but really to do, it is more difficult to find than the look.
First of all, Java in the creation of a source file before the first to build a package , and then I use the eclipse write Java (feel and pycharm style almost), and then create a new file, enter the corresponding code, I first entered the code is this
This is the first time the blogger code, run with errors, such as:
After Baidu tried several methods, the first is in Gamelaunch this class before adding a stastic, but there are other places error, the following errors:
And then tried another way, but has been error, and finally the help of the roommate, only to successfully solve the problem, first of all the class in front of the main change, replace the package name (of course, the front of the package with the same name, including all the class also to be removed), Then the following class of the front of the public to remove, you can run, with the source code:
Package HelloWorld;
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 am thinking 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 of the +guessp2");
guessp3 = p3.number;
System.out.println ("Player three guess is" +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 got it right?") +p2isright);
System.out.println ("Player three got it rigt?") +p3isright);
System.out.println ("Game is over!");
Break ;
}
else {
System.out.println ("Player has to try again!");
}
}
}
}
class player{
int number = 0;
Public void Guess () {
Number = (int) (Math.random () *10);
System.out.println ("I am Guessing" +number);
}
}
Public class helloworld{
Public static void Main (string[] args) {
guessgame game = new Guessgame ();
game.startgame ();
}
}
The results of the final operation are as follows:
So, a simple Java program is running successfully.
I conclude that the code example given in the book is actually running error may be because the code in the book is written in accordance with the Java code OJ above the format to write, but when it is written to consider their own files, so need to make corresponding changes. (Bo Master is a novice, you Big Brother don't take offense)
Java Beginner First Play--compile and run the simple program on the book (Guess the number games)