Java代码随机产生学号

自定义随机抽取学号java程序
(可自定义总人数与所需人数)
import java.io.;
import javax.swing.
;
import java.util.;
import jxl.
;
import java.awt.;
import java.awt.event.
;
public class ReadText
{
public static void main(String args[])
{
WindowStudent win=new WindowStudent();
win.setBounds(100,100,410,360);
win.setTitle(“随机点名”);
}
}
class WindowStudent extends JFrame implements ActionListener
{
Student student;
JTextField textA,textB;
JTextArea showArea;
JButton controlButton;
WindowStudent(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
student=new Student();
textA=new JTextField(5);
textB=new JTextField(5);
showArea=new JTextArea();
controlButton=new JButton(“随机点名”);
JPanel pNorth=new JPanel();
pNorth.add(new JLabel(“总人数:”));
pNorth.add(textA);
pNorth.add(new JLabel(“点名人数:”));
pNorth.add(textB);
pNorth.add(controlButton);
controlButton.addActionListener(this);
add(pNorth,BorderLayout.NORTH);
add(new JScrollPane(showArea),BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
try{
int a=Integer.parseInt(textA.getText().trim());
int b=Integer.parseInt(textB.getText().trim());
student.setA(a);
student.setB(b);
String Num=student.getNum();
showArea.append(“被点到的”);
showArea.append(Num+"\n");
}
catch(Exception ex){
showArea.append("\n"+ex+"\n");
}
}
}
class Student
{
int NumA,NumB;
public String getNum(){
int []a=GetRandomNumber.getRandomNumber(NumA,NumB);
return String.valueOf(java.util.Arrays.toString(a));
}
public void setA(int x){
NumA=x;
}
public void setB(int b){
NumB=b;
}
}
class GetRandomNumber
{
public static int []getRandomNumber(int max,int amount){
int []randomNumber=new int[amount];
int index=0;
randomNumber[0]=-1;
Random random=new Random();
while(index int number=random.nextInt(max)+1;
boolean isInArrays=false;
for(int m:randomNumber){
if(m==number)
isInArrays=true;
}
if(isInArrays ==false){
randomNumber[index]=number;
index++;
}
}
return randomNumber;
}
}
效果图如下

Java代码随机产生学号_第1张图片

你可能感兴趣的:(学习)