Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 1st, 2005, 3:29 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Number Guessing Game

Hi i'm supposed to make a number guessing game that does the following:

Quote:
What is your guess? (from 0 to 10) ? 8
That is wrong, try again. (Output this line in red - use a sound to signal the wrong answer)

What is your guess? (from 0 to 10) ? 5
That is wrong, try again.

What is your guess? (from 0 to 10) ? 4
That is right, it took 3 tries. (Output this line in green - use a sound to signal the correct answer)
This is how far i got:

import java.awt.*;
import hsa.Console;
public class guess
{
    static Console c = new Console ();
    static public void main (String[] args)
    {
        int g1;
        c.clear ();
        for (int x = 1 ; x <= 10 ; x++)
        {
            c.setTextColor (Color.black);
            c.print ("What is your guess? ");
            g1 = c.readInt ();
            if (g1 != x)
            {
                c.setTextColor (Color.red);
                c.println ("That is wrong, try again.");
                c.println ();
            }
            else
                if (g1 == x)
                {
                    c.setTextColor (Color.red);
                    c.println ("That is right, it took you " "tries.");
                }
        }
    }
}

can someone help me?
Dark Flare Knight is offline   Reply With Quote
Old Apr 2nd, 2005, 6:24 AM   #2
boon8623
Newbie
 
boon8623's Avatar
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0 boon8623 is on a distinguished road
why use a for loop?
why not use Random class?
thn compare the input with the generated num...
boon8623 is offline   Reply With Quote
Old Apr 2nd, 2005, 9:06 AM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Or Math.rand() (I believe that is a real method).
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 2nd, 2005, 9:14 AM   #4
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Well i haven't learned any of those yet. Otherwise i would use em.
Dark Flare Knight is offline   Reply With Quote
Old Apr 2nd, 2005, 10:30 AM   #5
Childe Roland
Newbie
 
Childe Roland's Avatar
 
Join Date: Mar 2005
Posts: 17
Rep Power: 0 Childe Roland is on a distinguished road
something like this should work:

Random value = new Random();
value = Math.abs(value.nextInt % 10 + 1);
Childe Roland is offline   Reply With Quote
Old Apr 2nd, 2005, 11:46 AM   #6
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
What do you mean by :
Quote:
Originally Posted by Dark Flare Knight
import hsa.Console;
??

use a for(;; ) loop. If x is what you'r trying to guess, stop incrementing it in your for loop. Do something like:
int i=0;
for(;;){
     if(gl==x){
        i++;
        System.out.println("Yeaapyy u found the number"+gl+"in"+i+"tries");
        break;
     }
     else {
           System.out.println("The number you entered is wrong");
           i++;  // this will give you the number of tries. Initiate it with 0 first
     }
}
That Console reader doesn't work on my comp. so you could try :
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

Also use the example given by Childe Roland to generate your number

Good luck!

edit: for(; - damn formating
__________________
Don't take life too seriously, it's not permanent !

Last edited by xavier; Apr 2nd, 2005 at 11:50 AM.
xavier is offline   Reply With Quote
Old Apr 2nd, 2005, 1:38 PM   #7
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Maybe i'm doing it wrong but its not working.

this is what i got so far:

import java.awt.*;
import hsa.Console;
public class guess
{
    static Console c = new Console ();
    static public void main (String[] args)
    {
        int g1;
        c.clear ();
        for (int x = 1 ; x <= 10 ; x++)
        {
            c.setTextColor (Color.black);
            c.print ("What is your guess? ");
            g1 = c.readInt ();
            int i = 0;
            for (;;)
            {
                if (gl == x)
                {
                    i++;
                    System.out.println ("Yeaapyy u found the number" + gl + "in" + i + "tries");
                    break;
                }
                else
                {
                    System.out.println ("The number you entered is wrong");
                    i++;
                }
            }
        }
    }
}
Dark Flare Knight is offline   Reply With Quote
Old Apr 2nd, 2005, 10:59 PM   #8
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Wink

Hey, listen, I personaly think that you have no ideea about what you'r doing. You just copyed the bit I gave; think for a change.

Honestly, .. go study....
__________________
Don't take life too seriously, it's not permanent !

Last edited by xavier; Apr 2nd, 2005 at 11:02 PM.
xavier is offline   Reply With Quote
Old Apr 3rd, 2005, 6:56 AM   #9
Childe Roland
Newbie
 
Childe Roland's Avatar
 
Join Date: Mar 2005
Posts: 17
Rep Power: 0 Childe Roland is on a distinguished road
i think what you should do is get a piece of paper and write out the logic of your program and see what it is that you have x doing.

I'm not really sure if you want it to do what it is doing and that can throw of the rest of your program.
Childe Roland is offline   Reply With Quote
Old Apr 3rd, 2005, 5:37 PM   #10
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
whats a for(;; ) used for?
Dark Flare Knight is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC