I am having a lot of trouble with the game I am supposed to be creating, so I'm going to put it out here for the many minds smarter about this stuff than I am to see if they can let me know whats wrong... I am brand new to programming, especially with Java. and I'm trying to create this Bulls and Cows game. There is supposed to be a menu which I have coded in, however, I don't know how to get the menu items to execute what they are supposed to in accordance with the game... any help with this endeavor would be much appreciated. Thank you very much in advance!
My code so far:
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class Main {
public void display_menu();
System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
System.out.print( "Selection: " );
}
public class Main() {
Scanner in = new Scanner ( System.in );
display_menu();
switch ( in.nextInt()) {
case 1:
System.out.println ( "Start New game - New Player" );
break;
case 2:
System.out.println ( "Start new Game - Same player" );
break;
case 3:
System.out.println ( "Here are the scores, highest to lowest: " );
break;
case 4:
System.err.println ( "Here's who has played the game!! " );
break;
case 5:
System.out.println ( "Here are the Top Scores " );
break;
case 6:
System.out.println ( "thanks for paying " );
break;
}}
class bull2Game{
public static void main(String args[]){
int i,j,bull=0,cow=0;int[] secretDigit,arr;
Scanner scan=new Scanner(System.in);
String msg, ans, playNextGame;
secretDigit=new int[4];
arr=new int[4];
System.out.println("Bulls and Cows");
System.out.println("This is an ancient game with guessing numbers.");
Random rand=new Random();
Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
do{
String secret="";
playNextGame="";
for(i=0;i<4;i++){
secretDigit[i]=rand.nextInt(10);
secret+=secretDigit[i];
}
System.out.println("A secret number of 4-digit has been draw!");
do{
do{
System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
ans=scan.nextLine().trim();
System.out.println(ans);
}
while(!regex.matcher(ans).find());
if(ans.equals("quit")){
System.out.println("The secret number is: " + secret);
}
else{
cow=0;
bull=0;
for(i=0;i<4;i++)
try{
arr[i]=Integer.parseInt(ans.substring(i,i+1));
if(arr[i]==secretDigit[i]) bull++;
}
catch(NumberFormatException nfe){}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
arr[j]=-1;
break;
}
cow-=bull;
msg="";
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!";
}
else{
if(bull==4)
msg="4 bulls! You are a genius!";
else{
if(cow==0){
if(bull==1)
msg="1 bull only.";
else
msg=bull + " bulls only.";
}
else if(bull==0){
if(cow==1)
msg="1 cow ";
else
msg=cow + " cows ";
}
else{
if(bull==1)
msg="1 bull and ";
else
msg=bull + " bulls and ";
if(cow==1)
msg+="1 cow.";
else
msg+=cow + " cows.";
}
}
}
System.out.println(msg);
}
}
while(bull!=4 && !ans.equals("quit"));
if(!ans.equals("quit")){
do{
System.out.print("Do you want to play a new game (Y/N)?");
playNextGame=scan.nextLine().trim();
System.out.println(playNextGame);
}
while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"));
}
}
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
System.out.println("Thank you for playing Bulls and Cows!");
}}
Category
Computers & Internet > Programming & Design
Does the posted code compile without errors?
Please copy and paste here the full text of any error messages.
Comments on the posted code:
1) there are two classes named: Main
2) the formatting has too much indentation in the last part
3) the }s do not line up vertically beneath the line with the starting "{"
4) there are two "}"s on the same line making it hard to read the nesting of the logic
5) there is an empty method defined
6) there are executable statements outside of any method
no, it does not compile without errors. the messages are as follows:
---------- Capture Output ----------
> "C:\j2sdk1.4.2_19\bin\javac.exe" bull2Game.java
bull2Game.java:6: <identifier> expected
System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
^
bull2Game.java:7: <identifier> expected
System.out.print( "Selection: " );
^
bull2Game.java:9: '{' expected
public class Main() {
^
bull2Game.java:126: '}' expected
4 errors
here is the code again, I've tried to the best of my ability to fix the errors you pointed out. I'm not sure about the class error... does it matter if one is a class main and the other is a public class main? or a public void class main?
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class Main {
public void display_menu();
System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
System.out.print( "Selection: " );
}
public class main() {
Scanner in = new Scanner ( System.in );
display_menu();
switch ( in.nextInt()) {
case 1:
System.out.println ( "Start New game - New Player" );
break;
case 2:
System.out.println ( "Start new Game - Same player" );
break;
case 3:
System.out.println ( "Here are the scores, highest to lowest: " );
break;
case 4:
System.err.println ( "Here's who has played the game!! " );
break;
case 5:
System.out.println ( "Here are the Top Scores " );
break;
case 6:
System.out.println ( "thanks for paying " );
break;
}
}
class bull2Game{
public static void main(String args[]){
int i,j,bull=0,cow=0;int[] secretDigit,arr;
Scanner scan=new Scanner(System.in);
String msg, ans, playNextGame;
secretDigit=new int[4];
arr=new int[4];
System.out.println("Bulls and Cows");
System.out.println("This is an ancient game with guessing numbers.");
Random rand=new Random();
Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
do{
String secret="";
playNextGame="";
for(i=0;i<4;i++){
secretDigit[i]=rand.nextInt(10);
secret+=secretDigit[i];
}
System.out.println("A secret number of 4-digit has been draw!");
do {
System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
ans=scan.nextLine().trim();
System.out.println(ans);
}
while(!regex.matcher(ans).find());
if(ans.equals("quit")){
System.out.println("The secret number is: " + secret);
}
else{
cow=0;
bull=0;
for(i=0;i<4;i++)
try{
arr[i]=Integer.parseInt(ans.substring(i,i+1));
if(arr[i]==secretDigit[i]) bull++;
}
catch(NumberFormatException nfe){}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
arr[j]=-1;
break;
}
cow-=bull;
msg="";
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!";
}
else{
if(bull==4)
msg="4 bulls! You are a genius!";
else{
if(cow==0){
if(bull==1)
msg="1 bull only.";
else
msg=bull + " bulls only.";
}
else if(bull==0){
if(cow==1)
msg="1 cow ";
else
msg=cow + " cows ";
}
else{
if(bull==1)
msg="1 bull and ";
else
msg=bull + " bulls and ";
if(cow==1)
msg+="1 cow.";
else
msg+=cow + " cows.";
}
}
}
System.out.println(msg);
}
}
while(bull!=4 && !ans.equals("quit"));
if(!ans.equals("quit")){
do{
System.out.print("Do you want to play a new game (Y/N)?");
playNextGame=scan.nextLine().trim();
System.out.println(playNextGame);
}
while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"));
}
}
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
System.out.println("Thank you for playing Bulls and Cows!");
}
does it matter if one is a class main and the other is a public class main?
It shouldn't be an error if the class names are spelled differently. main and Main are different. Normally the name of the class is related to what the class is used for.
How can there be classes named main and Main? Do you mean for one to be the constructor?
What is line 5 for? It looks like an empty method. Should lines 6 & 7 be in that method?
Lines 6 & 7 should be inside of a method
There should not be () after the class name on line 9
ok, fixed those mistakes... so how do I get the options in the menu I have created to execute what they are supposed to in the program, i.e. new game same player should execute playAgain, ect...
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class Main {
public void display_menu(); {
System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
System.out.print( "Selection: " );
}
public class main {
Scanner in = new Scanner ( System.in );
display_menu();
switch ( in.nextInt()) {
case 1:
System.out.println ( "Start New game - New Player" );
break;
case 2:
System.out.println ( "Start new Game - Same player" );
break;
case 3:
System.out.println ( "Here are the scores, highest to lowest: " );
break;
case 4:
System.err.println ( "Here's who has played the game!! " );
break;
case 5:
System.out.println ( "Here are the Top Scores " );
break;
case 6:
System.out.println ( "thanks for paying " );
break;
}
}
class bull2Game{
public static void main(String args[]){
int i,j,bull=0,cow=0;int[] secretDigit,arr;
Scanner scan=new Scanner(System.in);
String msg, ans, playNextGame;
secretDigit=new int[4];
arr=new int[4];
System.out.println("Bulls and Cows");
System.out.println("This is an ancient game with guessing numbers.");
Random rand=new Random();
Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
do{
String secret="";
playNextGame="";
for(i=0;i<4;i++){
secretDigit[i]=rand.nextInt(10);
secret+=secretDigit[i];
}
System.out.println("A secret number of 4-digit has been draw!");
do {
System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
ans=scan.nextLine().trim();
System.out.println(ans);
}
while(!regex.matcher(ans).find());
if(ans.equals("quit")){
System.out.println("The secret number is: " + secret);
}
else{
cow=0;
bull=0;
for(i=0;i<4;i++)
try{
arr[i]=Integer.parseInt(ans.substring(i,i+1));
if(arr[i]==secretDigit[i]) bull++;
}
catch(NumberFormatException nfe){}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
arr[j]=-1;
break;
}
cow-=bull;
msg="";
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!";
}
else{
if(bull==4)
msg="4 bulls! You are a genius!";
else{
if(cow==0){
if(bull==1)
msg="1 bull only.";
else
msg=bull + " bulls only.";
}
else if(bull==0){
if(cow==1)
msg="1 cow ";
else
msg=cow + " cows ";
}
else{
if(bull==1)
msg="1 bull and ";
else
msg=bull + " bulls and ";
if(cow==1)
msg+="1 cow.";
else
msg+=cow + " cows.";
}
}
}
System.out.println(msg);
}
}
while(bull!=4 && !ans.equals("quit"));
if(!ans.equals("quit")){
do{
System.out.print("Do you want to play a new game (Y/N)?");
playNextGame=scan.nextLine().trim();
System.out.println(playNextGame);
}
while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"));
}
}
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
System.out.println("Thank you for playing Bulls and Cows!");
}
}
how do I get the options in the menu I have created to execute
Are there any compiler errors left?
You will need to fix all of the compiler errors before you can execute the code for testing.
here is the list of errors now:
---------- Capture Output ----------
> "C:\j2sdk1.4.2_19\bin\javac.exe" bull2Game.java
bull2Game.java:11: invalid method declaration; return type required
display_menu();
^
bull2Game.java:12: illegal start of type
switch ( in.nextInt()) {
^
bull2Game.java:32: <identifier> expected
}
^
bull2Game.java:123: illegal start of type
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
^
bull2Game.java:123: <identifier> expected
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
^
bull2Game.java:124: <identifier> expected
System.out.println("Thank you for playing Bulls and Cows!");
^
6 errors
> Terminated with exit code 1.
; are used to end statements. Why is there one in the first error?
The statements from line 10 on look like they belong in a method, but are not in a method.
They immediately follow the definition of a class on line 9.
If the source in is a file named: bull2Game.java the first public class in that file should be the bull2Game class. What are the Main and main classes for?
You need to rethink what classes you are going to use and what their names should be.
The public class's name should match the name of the .java file and should be the one with the main() method.
eed to rethink what classes you are going to use and what their names should be.
The public class's name should match the name of th
im sorry, but I only kind of comprehend what you are saying... from line 10-32 is the menu I was asking about... I know there should be a method there, but I dont know what to declare, or how to implement the options of the menu to actually execute to do what they are supposed to... asked for the classes, im utterly lost on that bit...
I suggest that you put this program away for a bit and try writing a smaller, simpler program that has a class and a couple of methods. You need to understand how to define a class and how to define methods in that class before you can make progress with this larger program.
how to implement the options of the menu to actually execute to do what they are supposed to.
Get your code to compile and execute as is before worrying about adding features.
I'm done for tonight. see you tomorrow.
Alright, I did some reading up, and I have now put lines 10-32 in a method public void menuOptions()... after doing so I now have only three compile errors:
---------- Capture Output ----------
> "C:\j2sdk1.4.2_19\bin\javac.exe" bull2Game.java
bull2Game.java:123: illegal start of type
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
^
bull2Game.java:123: <identifier> expected
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
^
bull2Game.java:124: <identifier> expected
System.out.println("Thank you for playing Bulls and Cows!");
^
3 errors
> Terminated with exit code 1.
That looks like there could be a mismatch pairing of {s and }s
Check for proper pairing of the curly brackets.
Ok, I changed one of the brackets that I thought didn't belong... the bracket on 122 I placed on 127, which then puts that (while) on 123 on 122... and now im getting 7 compile errors:
---------- Capture Output ----------
> "C:\j2sdk1.4.2_19\bin\javac.exe" bull2Game.java
bull2Game.java:2: cannot resolve symbol
symbol : class Scanner
location: package util
import java.util.Scanner;
^
bull2Game.java:5: missing method body, or declare abstract
public void display_menu(); {
^
bull2Game.java:10: cannot resolve symbol
symbol : class Scanner
location: class Main
Scanner in = new Scanner ( System.in );
^
bull2Game.java:10: cannot resolve symbol
symbol : class Scanner
location: class Main
Scanner in = new Scanner ( System.in );
^
bull2Game.java:36: cannot resolve symbol
symbol : class Scanner
location: class Main.bull2Game
Scanner scan=new Scanner(System.in);
^
bull2Game.java:36: cannot resolve symbol
symbol : class Scanner
location: class Main.bull2Game
Scanner scan=new Scanner(System.in);
^
bull2Game.java:34: inner classes cannot have static declarations
public static void main(String args[]){
^
7 errors
> Terminated with exit code 1.
missing method body
public void display_menu();
The ; ends the definiton for the method. remove it.
j2sdk1.4.2_19
You need to update your JDK if you are using version 1.4. The Scanner class was added in version 1.5
After updating the java JDK all I have is this error:
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.7.0\bin\javac.exe" bull2Game.java
bull2Game.java:34: error: Illegal static declaration in inner class Main.bull2Game
public static void main(String args[]){
^
modifier 'static' is only allowed in constant variable declarations
1 error
> Terminated with exit code 1.
"inner class" means you have declared one class inside another (bull2Game is declared inside Main). In this case that's probably a mixup with the matching of the { and }
The main() method should be in the public class that is not nested within another class.
Did you change the source so the bull2Game (should be Bull2Game) class is a public class and is not inside of another class?
When you execute the program the command used should be: java Bull2Game
when the main() method is in the Bull2Game class.
So, I fixed it so that the classes are separate, however, when I run the program there is no menu... it just goes straight to the game... Now that I actually have no compile errors, and I can actually play the game, this is where I need help with the menu portion making each option execute its actual function...
heres the working code:
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
class Main {
public void display_menu() {
System.out.println( " 1) new game new player\n2) new game same player\n3) Sort by Score\n4) Sort by Name\n5) Display Top Score Report\n6) Quit Game " );
System.out.print( "Selection: " );
}
public void menuOptions() {
Scanner in = new Scanner ( System.in );
display_menu();
switch ( in.nextInt()) {
case 1:
System.out.println ( "Start New game - New Player" );
break;
case 2:
System.out.println ( "Start new Game - Same player" );
break;
case 3:
System.out.println ( "Here are the scores, highest to lowest: " );
break;
case 4:
System.err.println ( "Here's who has played the game!! " );
break;
case 5:
System.out.println ( "Here are the Top Scores " );
break;
case 6:
System.out.println ( "thanks for paying " );
break;
}
}}
class bull2Game{
public static void main(String args[]){
int i,j,bull=0,cow=0;int[] secretDigit,arr;
Scanner scan=new Scanner(System.in);
String msg, ans, playNextGame;
secretDigit=new int[4];
arr=new int[4];
System.out.println("Bulls and Cows");
System.out.println("This is an ancient game with guessing numbers.");
Random rand=new Random();
Pattern regex=Pattern.compile("^\\d{4}$|^quit$");
do{
String secret="";
playNextGame="";
for(i=0;i<4;i++){
secretDigit[i]=rand.nextInt(10);
secret+=secretDigit[i];
}
System.out.println("A secret number of 4-digit has been draw!");
do {
System.out.print("Please enter a 4-digit number (or type 'quit' to exit):");
ans=scan.nextLine().trim();
System.out.println(ans);
}
while(!regex.matcher(ans).find());
if(ans.equals("quit")){
System.out.println("The secret number is: " + secret);
}
else{
cow=0;
bull=0;
for(i=0;i<4;i++)
try{
arr[i]=Integer.parseInt(ans.substring(i,i+1));
if(arr[i]==secretDigit[i]) bull++;
}
catch(NumberFormatException nfe){}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(arr[j]==secretDigit[i]){
cow++;
arr[j]=-1;
break;
}
cow-=bull;
msg="";
if(cow==0 && bull==0){
msg="Neither a bull nor a cow!";
}
else{
if(bull==4)
msg="4 bulls! You are a genius!";
else{
if(cow==0){
if(bull==1)
msg="1 bull only.";
else
msg=bull + " bulls only.";
}
else if(bull==0){
if(cow==1)
msg="1 cow ";
else
msg=cow + " cows ";
}
else{
if(bull==1)
msg="1 bull and ";
else
msg=bull + " bulls and ";
if(cow==1)
msg+="1 cow.";
else
msg+=cow + " cows.";
}
}
}
System.out.println(msg);
}
}
while(bull!=4 && !ans.equals("quit"));
if(!ans.equals("quit")){
do{
System.out.print("Do you want to play a new game (Y/N)?");
playNextGame=scan.nextLine().trim();
System.out.println(playNextGame);
}
while(!playNextGame.toUpperCase().equals("Y") && !playNextGame.toUpperCase().equals("N"));
}
while (playNextGame.toUpperCase().equals("Y") && !ans.equals("quit"));
System.out.println("Thank you for playing Bulls and Cows!");
}
}
Why are there 2 classes? How are the methods in the Main class called?
When is an instance of the Main class created?
Could the Main class's methods be moved to the Bull2Game class?
should one be a public class? and I have no clue how they are called... the whole menu portion is what I am stumped on...