-
Problem in stoping the rpeating number
import BreezySwing.*;
import javax.swing.*;
import java.awt.*;
public class Lottery extends GBFrame{
private JLabel ALbl, BLbl, CLbl, DLbl, ELbl, FLbl, SLbl;
private IntegerField AIf, BIf, CIf, DIf, EIf, FIf, SIf;
private JButton startBtn, clearBtn;
public Lottery(){
// Lables
ALbl = addLabel("A", 1, 1, 1, 1);
BLbl = addLabel("B", 1, 2, 1, 1);
CLbl = addLabel("C", 1, 3, 1, 1);
DLbl = addLabel("D", 1, 4, 1, 1);
ELbl = addLabel("E", 1, 5, 1, 1);
FLbl = addLabel("F", 1, 6, 1, 1);
SLbl = addLabel("S", 1, 7, 1, 1);
//Intger Fields
AIf = addIntegerField(0, 2, 1, 1, 1);
BIf = addIntegerField(0, 2, 2, 1, 1);
CIf = addIntegerField(0, 2, 3, 1, 1);
DIf = addIntegerField(0, 2, 4, 1, 1);
EIf = addIntegerField(0, 2, 5, 1, 1);
FIf = addIntegerField(0, 2, 6, 1, 1);
SIf = addIntegerField(0, 2, 7, 1, 1);
SIf.setBackground(new Color(255, 255, 0));
//Buttons
startBtn = addButton("Start", 3, 1, 2, 1);
clearBtn = addButton("Clear", 3, 4, 2, 1);
// Size && visiblity && Title
setTitle("Lottery Number Generator By >>MujiCom<<");
setSize(500, 250);
setVisible(true);
}
public void buttonClicked(JButton btn){
if(btn == startBtn){
AIf.setNumber((int)(Math.random() * 98 + 1));
BIf.setNumber((int)(Math.random() * 98 + 1));
CIf.setNumber((int)(Math.random() * 98 + 1));
DIf.setNumber((int)(Math.random() * 98 + 1));
EIf.setNumber((int)(Math.random() * 98 + 1));
FIf.setNumber((int)(Math.random() * 98 + 1));
SIf.setNumber((int)(Math.random() * 98 + 1));
}else if(btn == clearBtn){
AIf.setNumber(0);
BIf.setNumber(0);
CIf.setNumber(0);
DIf.setNumber(0);
EIf.setNumber(0);
FIf.setNumber(0);
SIf.setNumber(0);
}
}
public static void main(String[] args){
new Lottery();
}
}
Iwould like to add a code that will not allow the programe to rpeat the numbers like A != B and B != C and C != D and ....
-
Well, using randomly generated numbers does not assure you that there will be no repeats.
You will get what are supposed to be "better" random numbers by using java.util.Random -
Random r = new Random();
AIf = r.nextInt();
BIf = r.nextInt();
etc.
if you want to limit to a range, such as 0-99, use modulo
AIf = r.nextInt() % 100;
Last edited by nspils; 01-03-2006 at 10:37 AM.
-
it says
C:\Documents and Settings\Mujtaba\Desktop\Lottery.java:57: incompatible types
found : int
required: BreezySwing.IntegerField
AIf = r.nextInt() % 100;
-
You have an "IntegerField" and r.nextInt() returns an int. How are you converting from an int to an IntegerField? From your code it appears that BreezySwing.IntegerField has a setNumber() method - so might you use AIf.setNumber( r.nextInt() )?
-
it works but it genrates negtiver numbers not btween 0-99 +
public void buttonClicked(JButton btn){
Random r = new Random();
if(btn == startBtn){
AIf.setNumber( r.nextInt() % 100);
BIf.setNumber( r.nextInt() % 100);
CIf.setNumber( r.nextInt() % 100);
DIf.setNumber( r.nextInt() % 100);
EIf.setNumber( r.nextInt() % 100);
FIf.setNumber( r.nextInt() % 100);
SIf.setNumber( r.nextInt() % 100 );
-
The Java implementation of the modulo operator has an inadequate implementation of modulo when it comes to negative numbers (the manner in which it works with 2's complement is incomplete). If the random number is negative, the formula
(n % 100) + 100;
will give the true "remainder" you are looking for.
-
thx for all the help
just now the totur of the subject said that he made a mistake and the number should be from 1-45 can show me how and postive
Similar Threads
-
By George in forum oracle.general
Replies: 0
Last Post: 04-01-2003, 12:00 AM
-
By George in forum oracle.general
Replies: 0
Last Post: 03-31-2003, 11:57 PM
-
By George in forum oracle.general
Replies: 0
Last Post: 03-31-2003, 11:56 PM
-
By rsmith555 in forum VB Classic
Replies: 1
Last Post: 03-09-2001, 08:51 AM
-
By Amrita in forum VB Classic
Replies: 11
Last Post: 02-08-2001, 06:41 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks