• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random numbers with a twist problem.  RSS feed

 
Jay Rounds
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

How would I code this problem, A button generates a random number between 40 and 70 and
displays it on the screen in a box or field (not sure what to use to display this) and then the button is disabled.
Then another button also generates a random number between 1 and 10 subtracting it from the
first random number and displays it on a screen in a box or field.

Example: Press button, Say random number 45 shows up on the screen. Disable button.
Then second button pressed, say the random number is 8, so 37 shows up on the screen in a box or field or ?

I am using NetBeans for this project. Here is the code I am using to generate the random numbers. I am using the same code for the 40 to 70 and 1 to 10, Just changing the ranges.

public class Randomizer
{
public static int generate(int min,int max)
{
return min + (int)(Math.random() * ((max - min) + 1));
}
public static void main(String[] args)
{
System.out.println(Randomizer.generate(1,10));
}
}

I am not sure what display type I should use either, ie Button, text feild, panel, pane???
cause I want the box of field to blink or flash when the second button is = to or less than 10. I want to have 9 boxes or fields to represent
the last number and blink in that box or field. So there will be 9 boxes or fields numbered 1,2,3,4,5,6,7,8 and 9 an 10 will have the same box and if 9 or 10 is the last number it will blink on that box.

Any Ideas?

Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Rounds wrote:Any Ideas?

Yes. First, you've definitely got the right idea by creating a Randomizer class. That means your separating the problem from your display. The Randomizer itself doesn't care whether you're using a button, a trowel or a piece of Rembrandt; it's just there to return a number.

What I would suggest is that you use Random.nextInt() rather than Math.random() though. It's much easier to follow, and the result will already be in the form you want it, so you don't have to cast.

The rest is simply screen design, and it seems you've already been told how to do it:
  • A button for the 40-70 number (and presumably a text field or label to display the result; or possibly just display the result in the button's label).
  • A button for the 1-10 number (and presumably, again, as above, something to display the result).
  • A field to display the result of the subtraction (and probably two more labels for the '-' and '=' signs).


  • Get that working. There'll be plenty of time for tweaking the results or adding fancy stuff like flashing later on.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!