求一个分组抽签程序,谢谢了,java
发布网友
发布时间:2022-05-20 13:00
我来回答
共2个回答
热心网友
时间:2023-10-22 07:17
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Ballot extends JFrame implements ActionListener,KeyListener{
//窗口组建,成员变量
JTextArea _txt;
JButton _ballot;
JTextField _print;
JScrollPane _panel;
int n_o_p;
int index;
int _index;
String[] names;
static Vector<Integer> num=new Vector<Integer>();
static Random r=new Random();
//
boolean num_print=true;
public static void main(String[] args) {
new Ballot();
}
public Ballot()
{
n_o_p=0;
index=0;
_index=0;
_txt=new JTextArea();
_txt.setText("请在上面输入人数,按Enter结束!");
_txt.setEditable(false);
_panel=new JScrollPane(_txt);
_ballot=new JButton("抽签");
_print=new JTextField(10);
this.setLayout(new BorderLayout());
this.add(_panel,BorderLayout.CENTER);
this.add(_print,BorderLayout.NORTH);
this.add(_ballot,BorderLayout.SOUTH);
_print.addKeyListener(this);
_ballot.addActionListener(this);
this.setVisible(true);
this.setSize(new Dimension(400,200));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==_ballot)
{
if(index==n_o_p&&_index!=index)
{
boolean _b=true;
while(_b)
{
int _i=r.nextInt(index);
for(int i=0;i<num.size();i++)
{
if(_i==num.get(i))
{
num.remove(i);
_txt.append("\r\n序号"+_i+" 姓名:"+names[_index]);
_index++;
_b=false;
}
}
}
}
if(_index==index)
{
File f=new File(System.getProperty("user.dir")+"/Save.txt");
if(f.exists())
{
try {
OutputStreamWriter ow=new OutputStreamWriter(new FileOutputStream(f));
ow.write(_txt.getText());
ow.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else
{
try {
f.createNewFile();
OutputStreamWriter ow=new OutputStreamWriter(new FileOutputStream(f));
ow.write(_txt.getText());
ow.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
_txt.setText("信息以保存至"+f.getPath()+"下!");
}
}
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(num_print){
if(_print.getText().matches("[0-9]+"))
{
num_print=false;
n_o_p=Integer.parseInt(_print.getText());
names=new String[n_o_p];
_print.setText("");
_txt.setText("还需要输入"+n_o_p+"个人名!按回车完成单个人名输入!");
}else
{
_txt.setText("输入的人数非法!请重新输入!");
}
}else
{
if(_print.getText()!=null&&!_print.getText().equals("")&&index!=n_o_p)
{
names[index]=_print.getText();
index++;
_txt.setText("还需要输入"+(n_o_p-index)+"个人名!按回车完成单个人名输入!");
}
}
if(index==n_o_p)
{
for(int i=0;i<index;i++)
{
num.add(i);
}
_txt.setText("请点击按钮进行抽签!");
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
热心网友
时间:2023-10-22 07:18
请把需求写详细些追问有N队参加比赛,求一个可以对N队进行分组的程序
追答分组有什么要求吗?还是随便分,比如所有人都在A组?