Welcome to Dream.In.Code
Become a Java Expert!

Join 150,422 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,099 people online right now. Registration is fast and FREE... Join Now!




Code Help - Guessing Game

 
Reply to this topicStart new topic

Code Help - Guessing Game

Runesmith
14 Mar, 2008 - 08:10 PM
Post #1

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Hello everyone! I'm trying to teach myself how to code in Java, but I'm currently stuck at one of my book's practice questions. The first part was to write an outline for a guessing game program (which I think I did correctly), and the second part is to fill in the three methods at the end (the parts I'm having problems with). Any help would be appreciated!

CODE

import javax.swing.JOptionPane;
public class GuessingGame {
     public static void man(String[] args) {
       int guess;
       int target = getnum();
       do{
           guess = getguess();
           checkguess(guess,target);
           }while (guess != target);
           }
public static int getnum(){
/** Return a random integer from 1-100.

}
public static int getguess(){
/** Asks the user for their guess, an integer from 1-100.
  * Then it returns the guess.

}
public static void checkguess(int g, int t){

/** Compares the user’s guess as G with the random target
* as T. Then prints a message stating whether the
* guess is too high, too low, or an exact match
* which should end the game.
   }

}


This post has been edited by Runesmith: 14 Mar, 2008 - 08:11 PM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Code Help - Guessing Game
15 Mar, 2008 - 09:49 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

code.gif
User is offlineProfile CardPM
+Quote Post

Runesmith
RE: Code Help - Guessing Game
15 Mar, 2008 - 05:46 PM
Post #3

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Of course. I apoligize; here is the updated code. The one that is giving me the most trouble is the third method, but I think I have the general gist of it.

CODE

import javax.swing.JOptionPane;
public class GuessingGame {
     public static void man(String[] args) {
       int guess;
       int target = getnum();
       do{
           guess = getguess();
           checkguess(guess,target);
           }while (guess != target);
           }
public static int getnum(){
  (int)((int))’1’ + Math.random() * ((int)’100’ – (int)’a’ + 1);
/** Return a random integer between 1 and 100.  
}
public static int getguess(){
  String guessString = JOptionPane.showInputDialog(
  “Enter a number between 1 and 100:”);
  Int guess = Integer.parseInt(guessString);
  System.out.print(guess + “ is your inputted number.”);
/** Ask the user to input a number between 1 and 100,
* and then return it.
}
public static void checkguess(int g, int t){
    int g = getnum
    Int t = getguess
    If (g < t)
    System.out.print(G + “ is too low.”);
Else if (g > t)
    System.out.print(G + “ is too high.”);
Else if (g = t)
    System.out.print(G + “ is a perfect match.”);
/** Compares the user’s guess as G with the random target
* as T. Then prints a message stating whether the
* guess is too high, too low, or an exact match
* which should end the game.
   }
}

User is offlineProfile CardPM
+Quote Post

Runesmith
RE: Code Help - Guessing Game
16 Mar, 2008 - 03:52 PM
Post #4

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

I've updated/fixed my previous code, but it still isn't working right. The third method is still giving me the most trouble. Any assistance is appreciated!

CODE

import javax.swing.JOptionPane;
public class GuessingGame {
     public static void man(String[] args) {
       int guess;
       int target = getnum();
       do{
           guess = getguess();
           checkguess(guess,target);
           }while (guess != target);
           }
public static int getnum(){
  return getRandomCharacter('1', '100');
/** Return a random integer between 1 and 100.  
}
public static int getguess(){
  String guessString = JOptionPane.showInputDialog(
  “Enter a number between 1 and 100:”);
  Int guess = Integer.parseInt(guessString);
  System.out.print(guess + “ is your inputted number.”);
/** Ask the user to input a number between 1 and 100,
* and then return it.
}
public static void checkguess(int g, int t){
    g = guess;
    t = target;
    If (g < t)
    System.out.print(G + “ is too low.”);
Else if (g > t)
    System.out.print(G + “ is too high.”);
Else if (g = t)
    System.out.print(G + “ is a perfect match.”);
/** Compares the user’s guess as G with the random target
* as T. Then prints a message stating whether the
* guess is too high, too low, or an exact match
* which should end the game.
   }
}

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Code Help - Guessing Game
16 Mar, 2008 - 05:08 PM
Post #5

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
That is because you copy and paste the code from a document processing software like MS WORD or Open Office Writer.

Anyway: Most of your code is in uppercase, and Java being case sensitive you will need to change the case wherever required.

The strings inside certain statements like System.out.print(guess + “ is your inputted number.”); are not put inside standard ASCII quotes " " . So you need to change that.


The third method is still giving me the most trouble. If you could please explain the errors, we could find quicker solutions to your problems.

Hope that helps :)
User is offlineProfile CardPM
+Quote Post

Runesmith
RE: Code Help - Guessing Game
16 Mar, 2008 - 06:57 PM
Post #6

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Got it. smile.gif I fixed the code to be compiler-friendly. The only two errors I'm getting are:

1. The first method is giving me a host of errors: "not a statement", "; expected", "unclosed character literal", etc. For this method, I am supposed to have the code generate a random number between 1 and 100.

3. I'm getting just one error for the last method. For the "else if (g = t)", it says that it is receiving an integer, when it should be receiving a boolean. I know boolean is for true/false statements, but I can't figure out how to implement it into this code. For the third method, I am supposed to: 'compare the user’s guess as G with the random target as T. Then print a message stating whether the guess is too high, too low, or an exact match. An exact match should end the game.'


CODE

package guessinggame;
import javax.swing.JOptionPane;
public class Main {
     public static void guessinggame(String[] args) {
       int guess;
       int target = getnum();
       do{
           guess = getguess();
           checkguess(guess,target);
           }while (guess != target);
           }
public static int getnum(){
  (int)((int)'1' + Math.random() * ((int)'100' - (int)'1' + 1);
  }
public static int getguess(){
  String guessString = JOptionPane.showInputDialog(
  "Enter a number between 1 and 100:");
  int guess = Integer.parseInt(guessString);
  System.out.print(guess + " is your inputted number.");

}
public static void checkguess(int g, int t){
    if (g < t)
    System.out.print(g + " is too low.");
else if (g > t)
    System.out.print(g + " is too high.");
else if (g = t)
    System.out.print(g + " is a perfect match.");

   }

}

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Code Help - Guessing Game
17 Mar, 2008 - 03:27 AM
Post #7

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
To your first point :
Since, you have defined the following method to return an int.
It should be modified from:
CODE

public static int getnum(){
  (int)((int)'1' + Math.random() * ((int)'100' - (int)'1' + 1);
}


to:
CODE

public static int getnum(){
  return (int) (1 + Math.random() * (100 - 1 + 1)); //check the braces
}



See, the keyword return.

Also, when using integers you do not need to use ' ', these are only for characters.

To your third point ;) :
else if (g = t). To check the equality of values in Java you need to use the "==" operator. What you used here, is the assignment operator.

So, it should be like this : else if (g == t)


Also, your method getguess() should be returning an int.

add this as the last line of the code in that method: return guess;


One last thing : public static void guessinggame(String[] args)
if you think the name of the this method will make the program run, you are incorrect. To run a program you need to name the entry point of the program as main()

Change the name so it looks like : public static void main(String[] args)

Hope that helps you, but try to understand to learn faster. :)

This post has been edited by letthecolorsrumble: 17 Mar, 2008 - 03:28 AM
User is offlineProfile CardPM
+Quote Post

Runesmith
RE: Code Help - Guessing Game
17 Mar, 2008 - 08:43 AM
Post #8

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Thanks, Letthecolorsrumble! I'm not getting errors with the code any more, but the program doesn't want to run now. I keep getting a "No main classes" error when I use this:

CODE

package guessinggame;
import javax.swing.JOptionPane;
public class Main {
     public static void Main(String[] args) {
       int guess;
       int target = getnum();
       do{
           guess = getguess();
           checkguess(guess,target);
           }while (guess != target);
           }
public static int getnum(){
return (int) (1 + Math.random() * (100 - 1 + 1));
  }
public static int getguess(){
  String guessString = JOptionPane.showInputDialog(
  "Enter a number between 1 and 100:");
  int guess = Integer.parseInt(guessString);
  System.out.print(guess + " is your inputted number.");
  return guess;
}
public static void checkguess(int g, int t){
    if (g < t)
    System.out.print(g + " is too low.");
else if (g > t)
    System.out.print(g + " is too high.");
else if (g == t)
    System.out.print(g + " is a perfect match.");

   }

}


I have the main classes' path set in the properties window and everything. Any idea what could be wrong?
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Code Help - Guessing Game
17 Mar, 2008 - 09:14 AM
Post #9

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Well the main method's name should be all in lowercase. And I am glad I could be helpful. :)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month