「java随机学号」java按学号排序
今天给各位分享java随机学号的知识,其中也会对java按学号排序进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、如何编写一个随机出学号的Java程序
- 2、java产生一个随机数和学号相差4
- 3、java 假设一个班级共35人, 学号从1~35, 请编写程序进行随机学号抽取, 每个学号不
- 4、如何用java语言编写一个叫学号器,比如班里有五十个同学,按一下按钮,随机叫出一个同学的名字!求详细代
如何编写一个随机出学号的Java程序
import java.util.*;
public class RandomStudentId{
private int studentID;
private Random rand = new Random();
//private Set s = new HashSet();
public void RandID(){
studentID = rand.nextInt(1000); //这里写多少,就是从0到多少的随机数字
}
public int getID(){
return studentID;
}
public String toString(){
return "Student ID: " + studentID;
}
public static void main(String[] args){
RandomStudentId rsi = new RandomStudentId();
for(int i=0; i100; ++i){
rsi.RandID();
System.out.println("NO." + rsi.getID());
}
}
}
java产生一个随机数和学号相差4
如果你的学号是连续的,那是可以考虑做的
例如你的学号是 14108101xx----14108102xx
那么你产生的随机数可以定义为
int stuNo = Math.random(100)+1410810100;//学号
int randomNo = stuNo + 4;// 与学号相差4的数
如果有什么疑问可以继续追问。
java 假设一个班级共35人, 学号从1~35, 请编写程序进行随机学号抽取, 每个学号不
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SelectNo {
public static void main(String[] args) {
//将1-35号码放入List集合
ListString list = new ArrayListString();
for (int i = 1; i 36; i++) {
list.add(""+i);
}
//新建一个List集合用来存放抽出来的号码
ListString newList = new ArrayListString();
//随机抽取list集合中的一个元素,抽出后删除
Random rd = new Random();
int count = list.size();//集合中剩余号码
while (count 0) {
int index = rd.nextInt(count);//抽出的号码的位置
//放入新的集合
newList.add(list.get(index));
//删除原有集合的元素
list.remove(index);
count--;//原有集合少了一个
}
//输出号码
for (int i = 0; i newList.size(); i++) {
System.out.print(newList.get(i)+"\t");
//每五个换行
if(i%5 == 4){
System.out.println("");
}
}
}
}
如何用java语言编写一个叫学号器,比如班里有五十个同学,按一下按钮,随机叫出一个同学的名字!求详细代
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test extends JFrame{
String[] student;
JLabel jl;
test(String[] s){
student=s;
JPanel jp=new JPanel();
JButton jb=new JButton("叫号");
jl=new JLabel();
jp.add(jb);
jp.add(jl);
this.getContentPane().add(jp);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jl.setText(student[(int)Math.floor(Math.random()*50)]);
}
});
this.setTitle("叫号器");
this.setSize(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args)throws Exception {
String[] student=new String[50];
for (int i = 0;i student.length; i++) {
student[i] = "小"+(int)Math.floor(Math.random()*50);
}
new test(student);
}
}
关于java随机学号和java按学号排序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。