📄 lucky.java
字号:
import java.awt.*;
import java.awt.event.*;
class Balll extends Frame implements ActionListener {
//定义程序界面组件
Button OKBtn = new Button("OK");
TextField text1 = new TextField(25);
TextField text2 = new TextField(25);
TextField text3 = new TextField(25);
TextField text4 = new TextField(25);
TextField text5 = new TextField(25);
//定义int型变量以存放随机数
int num[] =new int[35];
int temp[]=new int[35];
int box;
//定义数组以存放每组数列
String str1="";
String str2="";
String str3="";
String str4="";
String str5="";
//构造方法
Balll() {
//初始化程序界面
setVisible(true); //设置窗口可见
setBounds(70,80,200,300); //设置界面长宽及位置
setLayout(new GridLayout(6,1)); //设布局为GridLayout布局
//向容器中添加组件
add(text1);
add(text2);
add(text3);
add(text4);
add(text5);
add(OKBtn);
//使按钮响应按钮事件
OKBtn.addActionListener(this);
//适配器(使窗口上的关闭按钮生效)
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == OKBtn) {
//清空字符串
str1="";
str2="";
str3="";
str4="";
str5="";
for(int i=0;i<35;i++){
num[i]=(int)(Math.random()*10000); //生成35个随机数并转换成int型存入数组num中
temp[i]=i; //将数组num的下标存入数组temp中
}
//对num中存放的随机数进行比较 并对数组temp排序,这样数组temp中就是将1-35随机排列的数列
for(int i=0;i<35;i++){
for(int j=0;j<35;j++){
if(num[i]<num[j]) {
box=temp[j];
temp[j]=temp[i];
temp[i]=box;
}
}
}
//将随机数组成的数列拆分成五组,并分别存入不同的字符串中
myIndex(temp, 0, 7);
myIndex(temp, 7,14);
myIndex(temp,14,21);
myIndex(temp,21,28);
myIndex(temp,28,35);
for(int i=0;i<35;i++){
if(i<7) {
//调用myIndex方法
//将个位数前加半角空格,以便与十位数对齐
if(temp[i]+1<10) {
str1+=" 0"+(temp[i]+1)+" ";
} else {
str1+=" "+(temp[i]+1)+" ";
}
} else if(i<14) {
if(temp[i]+1<10) {
str2+=" 0"+(temp[i]+1)+" ";
} else {
str2+=" "+(temp[i]+1)+" ";
}
} else if(i<21) {
if(temp[i]+1<10) {
str3+=" 0"+(temp[i]+1)+" ";
} else {
str3+=" "+(temp[i]+1)+" ";
}
} else if(i<28) {
if(temp[i]+1<10) {
str4+=" 0"+(temp[i]+1)+" ";
} else {
str4+=" "+(temp[i]+1)+" ";
}
} else if(i<35) {
if(temp[i]+1<10) {
str5+=" 0"+(temp[i]+1)+" ";
} else {
str5+=" "+(temp[i]+1)+" ";
}
}
}
//将字符串中的数列显示在文本框中
text1.setText(str1);
text2.setText(str2);
text3.setText(str3);
text4.setText(str4);
text5.setText(str5);
}
}
public void myIndex(int a[], int m,int n) {
//将同一组的数列进行排序
int box;
for(int i=m;i<n;i++){
for(int j=m;j<n;j++){
if(a[i]<a[j]) {
box=a[j];
a[j]=a[i];
a[i]=box;
}
}
}
}
}
public class Lucky {
public static void main(String args[]){
Balll win= new Balll();
win.pack();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -