![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Number Guessing Game
Hi i'm supposed to make a number guessing game that does the following:
Quote:
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? |
|
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2005
Posts: 3
Rep Power: 0
![]() |
why use a for loop?
why not use Random class? thn compare the input with the generated num... |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Or Math.rand() (I believe that is a real method).
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Well i haven't learned any of those yet. Otherwise i would use em.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2005
Posts: 17
Rep Power: 0
![]() |
something like this should work:
Random value = new Random(); value = Math.abs(value.nextInt % 10 + 1); |
|
|
|
|
|
#6 | |
|
Professional Programmer
|
What do you mean by :
Quote:
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
}
}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. |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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++;
}
}
}
}
} |
|
|
|
|
|
#8 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Mar 2005
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
whats a for(;; ) used for?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|