![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
It's an infinite loop that can only be broken out of by a break; statement.
|
|
|
|
|
|
#12 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Quote:
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? |
|
|
|
|
|
|
#13 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#14 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#15 |
|
Professional Programmer
|
Well, just replace the BufferedReader with your Console. they seem to be equivalent.
instead of : static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static Console in = new Console (); It should work ..
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#16 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|