java产⽣不重复随机学⽣学号_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);
java valueofshowArea=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.Text().trim());
int b=Integer.Text().trim());
student.setA(a);
student.setB(b);
String Num();
showArea.append(“被点到的”);
showArea.append(Num+"\n");
}
catch(Exception ex){
showArea.append("\n"+ex+"\n");
}
}
}
class Student
{
int NumA,NumB;
public String getNum(){
int []RandomNumber(NumA,NumB); return String.valueOf(java.String(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(indexint Int(max)+1;
boolean isInArrays=false;
for(int m:randomNumber){
if(m==number)
isInArrays=true;
}
if(isInArrays ==false){
randomNumber[index]=number;
index++;
}
}
return randomNumber;
}
}
效果图如下