Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 3rd, 2005, 7:01 PM   #11
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
It's an infinite loop that can only be broken out of by a break; statement.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 3rd, 2005, 9:54 PM   #12
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
Quote:
Originally Posted by xavier
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....
Sorry i didn't understand what you were saying earlier. I got the program running but i still need to fix something. Here is the code:

import java.awt.*;
import hsa.Console;
public class guess
{
    static Console c = new Console ();
    static public void main (String[] args)
    {
        int g1, i = 0;
        char play;
        c.clear ();
        for (int x = 0 ; x < 11 ; x++)
        {
            c.setTextColor (Color.black);
            i++;
            do
            {
                c.print ("What is your guess? ");
                g1 = c.readInt ();
                if (g1 >= 10 || g1 <= 0)
                    c.println ("Error! Please enter a number between 0 and 10");
            }
            while (g1 >= 10 || g1 <= 0);
            for (;;)
            {
                if (g1 == x)
                {
                    c.println ("You got the number " + g1 + " in " + i + " tries.");
                    break;
                }
                else
                {
                    c.println ("The number you entered is wrong.");
                    break;
                }

            }

        }
    }
}

I want the program to get a random number between 0 and 10 but the number this program is guessing is the same all the time. If i run it, after 5 tries, the number is always 5 and when it first starts, the number is always 0. I'm not really sure how to use the Math.random () if thats what is to be used here. Can someone tell me what to do?
Dark Flare Knight is offline   Reply With Quote
Old Apr 4th, 2005, 12:25 AM   #13
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
Here's how I did your problem. I don't have the hsa.console pakage to import so I used java.io.
import java.awt.*;
import java.io.*;
import java.util.*;

public class guess
{
 
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static public void main (String[] args)
    {


        Random r=new Random();
       	int x= Math.abs(r.nextInt(10));
       	System.out.println(x);   // just so u see the number for testing

       	int g1=0;


            System.out.println ("The number is between 0 and 10 ");
			System.out.println ("Try and guess it!");


            int i = 0;
            for (;;)
            {
			try{
			String s=in.readLine();
            		if(s.equals("exit"))break;        //to exit if you'r bored :)
            		else	g1 = Integer.parseInt(s);
           		}  catch(IOException e){e.printStackTrace();}

                if (g1 == x)
                {
                    i++;
                    System.out.println ("Yeaapyy u found the number " + g1 + " in " + i + " tries");
                    break;
                }
                else
                {
                    System.out.println ("The number you entered is wrong");
                    i++;
                }
            }
        }
    }

You can just modify it to fit your needs. You see there nextInt(10). It will generate numbers from 0 to 9. So if you have nextInt(n) .... it generates numebers between 0 an n-1.

Hope it's all clear from now on. All the best to you.
__________________
Don't take life too seriously, it's not permanent !

Last edited by xavier; Apr 4th, 2005 at 12:29 AM.
xavier is offline   Reply With Quote
Old Apr 4th, 2005, 7:29 AM   #14
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
there's just 1 problem with that, i haven't yet learned any other packages except hsa.Console (); so i don't really know how they work.

Last edited by Dark Flare Knight; Apr 4th, 2005 at 7:33 AM.
Dark Flare Knight is offline   Reply With Quote
Old Apr 4th, 2005, 10:32 AM   #15
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
Well, just replace the BufferedReader with your Console. they seem to be equivalent.
instead of :
 static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
use
static Console in = new Console ();

It should work ..
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Apr 4th, 2005, 3:40 PM   #16
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
i got it to work with only using hsa.Consoloe ();. I'll post the code here later. Now i gotta finish the check book assignment.

Here it is:

import java.awt.*;
import hsa.Console;
public class guess
{
    static Console c = new Console ();
    static public void main (String[] args)
    {
        int g1, i = 0;
        char play;
        c.clear ();
        for (int x = 0 ; x <= 10 ; x++)
        {

            x = (int) (Math.random () * 10) + 0;
            c.setTextColor (Color.black);
            i++;
            do
            {
                c.print ("What is your guess? ");
                g1 = c.readInt ();
                if (g1 > 10 || g1 <= 0)
                    c.println ("Error! Please enter a number between 0 and 10");
            }
            while (g1 > 10 || g1 <= 0);
            {
                if (g1 == x)
                {
                    c.println ("You got the number " + g1 + " in " + i + " tries.");
                    break;
                }
                else
                {
                    c.println ("The number you entered is wrong.");
                }
            }
        }
    }
}

Last edited by Dark Flare Knight; Apr 5th, 2005 at 10:04 AM.
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 4:48 PM.

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