java隨機點名系統 java點名系統怎麼寫
- 綜合知識
- 關注:1.6W次
給樓主改了一下
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
public class RandomClassgui extends WindowAdapter implements ActionListener{
Frame f = new Frame("點名系統");
Button startB;
public void display() {
f.setSize(300,200);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
startB = new Button("開始點名");
startB.addActionListener(this);
f.addWindowListener(this);
f.add(startB);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startB);
String[] namelist={"小明","小白","小黑","小黃","小蘭","小草","小麗","兔子","巫妖","船長","劍聖","冰女"};
int randomNum=(int)(Math.random()*namelist.length);
System.out.println(namelist[randomNum]);
}
public static void main(String args[]){
new RandomClassgui().display();
}
}
其實沒有改太多的地方
只是加了一句
f.add(startB);
2.java的陣列做一個點名系統,求程式碼import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class test {
static String[] arr;
static boolean flag=true;
static Lista;
static JTextField jtf;
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setLayout(new GridLayout(1,2));
JButton button = new JButton("抽獎");
jtf = new JTextField();
frame.add(jtf);
frame.add(button);
a = new ArrayList();
arr = new String[]{"張三","李四","王五"};
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i=0;
fond:
while(true){
String str = arr[(int)(Math.random()*arr.length)];
for(String b:a){
if(b.equals(str)){
if(i>=arr.length){
jtf.setText("沒有人了");
break fond;
}
continue fond;
}
i++;
}
a.add(str);
jtf.setText(str);
break;
}
}
});
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
3.隨機點名系統 javaimport java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class test_random extends JFrame implements ActionListener{ public static void main(String[] args) { test_random t = new test_random(); t.init(); } public void init(){ initListDate(); but = new JButton("開始點名"); but.setBounds(100,150,100,40); but.addActionListener(this); label = new JLabel("隨機點名"); label.setBounds(60,20,300,40); label.setFont(new Font("楷體",Font.BOLD,40)); show = new JLabel(""); show.setBounds(110,80,200,30); show.setFont(new Font("楷體",Font.BOLD,30)); reset = new JButton("重新點名"); reset.setBounds(203,246,90,25); reset.addActionListener(this); add(but); add(label); add(show); add(reset); setLayout(null); setVisible(true); setResizable(false); setBounds(100,100,300,300); setTitle("點名"); (JFrame.EXIT_ON_CLOSE); } public void initListDate(){ //在這個地方新增姓名就可以了 list.add("劉天廷"); list.add("孔老二"); list.add("張三"); list.add("李四"); list.add("王五"); list.add("麻六"); list.add("馮七"); list.add("京八"); list.add("茅臺九"); } public void actionPerformed(ActionEvent e) { /*確保每人選一次*/ Object obj = e.getSource(); if(obj == but){ if(list.size()==0){ JOptionPane.showMessageDialog(null,"沒有人了!"); return ; }else{ Random ran = new Random(); int num = ran.nextInt(list.size()); show.setText(list.get(num)); list.remove(num); } } if(obj == reset){ show.setText(""); initListDate(); } } private JButton but ; private JLabel label; private JLabel show; private JButton reset; private List list = new ArrayList(); }。
4.java隨機點名系統給樓主改了一下import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;public class RandomClassgui extends WindowAdapter implements ActionListener{ Frame f = new Frame("點名系統"); Button startB; public void display() { f.setSize(300,200); f.setLocation(200,140); f.setBackground(Color.lightGray); startB = new Button("開始點名"); startB.addActionListener(this); f.addWindowListener(this); f.add(startB); f.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==startB); String[] namelist={"小明","小白","小黑","小黃","小蘭","小草","小麗","兔子","巫妖","船長","劍聖","冰女"}; int randomNum=(int)(Math.random()*namelist.length); System.out.println(namelist[randomNum]); } public static void main(String args[]){ new RandomClassgui().display(); }} 其實沒有改太多的地方只是加了一句f.add(startB);。
5.JAVA程式設計(隨機點名程式),高手來程式碼如下,需要建立兩個txt,一個為a.txt,用來儲存原有名單,另外一個為b.txt用來儲存點名後的名單,在執行時可選,是否繼續上次點名還是開始新的點名, 程式完整,放入兩個txt檔案後即可執行,請檢測import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Student { List stuList = new ArrayList(); /** * 點名 * @throws IOException */ public void calling() throws IOException{ while(true){ System.out.println("點名請按【Y】,退出請按【N】:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); if(input.toLowerCase().equals("y")){ int noNum = (int) (Math.random()*this.stuList.size()); String name = (String) this.stuList.get(noNum); this.stuList.remove(noNum); System.out.println("當前點名使用者為:"+name); }else if(input.toLowerCase().equals("n")){ String s; File f=new File("b.txt"); FileOutputStream fos =new FileOutputStream(f); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos)); while (stuList.size() != 0) { String name = (String)stuList.get(0); System.out.println("存入name="+name); bw.write(name); bw.newLine(); stuList.remove(0); } bw.flush(); bw.close(); break; } } } /** * 讀入學生資訊 * @param fileName * @return * @throws Exception */ public void readStudent(String fileName) throws Exception { Scanner sc = new Scanner(new File(fileName)).useDelimiter("n"); int count = 0; while(sc.hasNext()){ count++; String name = sc.next(); System.out.println(count+"="+name); this.stuList.add(new String(name)); } } /** * 存入學生資訊 * @param args * @throws IOException */ public static void main(String[] args) throws Exception { Student stu = new Student(); System.out.println("開始新的點名【1】,繼續上次點名【2】:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); String newFileName = "a.txt"; String oldFileName = "b.txt"; if(input.toLowerCase().equals("1")){ stu.readStudent(newFileName); }else if(input.toLowerCase().equals("2")){ stu.readStudent(oldFileName); } stu.calling(); }}。
6.JAVA程式設計(隨機點名程式),高手來按照你第2點描述,你僅僅是想實現隨即順序點名而已,並不是隨即點名。
我給你個解決方案:在每次點中一名學生後,將java0803_students.txt檔案中做個標示,表示該學生已經在本輪點名中被點過。直到所有學生都點到後情況這個表示,準備下一輪點名做標記。
下面我給你一段虛擬碼,包含了 random 的使用方法:public class CallStudents(){ private static Random random = new Random(); public void call(){ // 重新設定學生被點名的標示(根據點名情況) reset(); long value = -1; while (true) { value = random.nextInt(maxValue + 1); // maxValue 是你當前有學生的最大數目//在這裡讀取當前隨即號對應的學生記錄 String student = getStudentByID(value); //判斷該學生是否被點過名 if(!checkCalled(student)){ // 設定已經點過名 setCalled(student); // 在控制檯上顯示被點名的學生資訊 system.out.println(student); // 結束迴圈 break; } } } private String getStudentByID(long id){ // 這個是讀取檔案,需要你自己實現的 // 返回 java0803_students.txt 檔案中對應的學生記錄 }private boolean checkCalled(String student){ // 至於是什麼樣的規則,有你自己定義 // 我這裡給你舉個例子 // 如果這條學生記錄中包含 一個字元為 called 的話,表明這名學生被點過名 if(student.indexOf("called") != -1){ retrn true; } return false; }private void setCalled(String student){ // 至於是什麼樣的規則,有你自己定義 // 我這裡給你舉個例子 // 設定學生已被點名 student += "_called"; // 將這個值重新寫回到你的java0803_students.txt 檔案中,覆蓋願資訊! }private void reset(){ // 判斷是否所有學生都被點過名,如果點過名,清空所有學生被點名的標記 // 這個實現有你自己定義, // 這裡我給你舉個例子 // 讀取檔案,判斷每一個學生資訊是否都標有 called // 如果都有,執行刪除所有 called 的操作,並儲存檔案。 // 如果只有部分沒有,則表明本輪點名沒有結束,不做任何操作。
}public static void main(String[] args){String con = // 接受控制檯資訊 if(con.equals("y")){ CallStudents call = new CallStudents(); call.call(); } if(con.equals("n")){ // 結束程式 }}}。
7.Java怎麼用迴圈做一個點名機public class CheckName(){
private String[] name=new String[50];//裡面寫上你們班的人數,比如說50
public static void main(String args[]){
Scanner in=new Scanner(System.out);
for(int i=1;i<=50;i++){
System.out.println("輸入第"+i+"個人名");
String[i-1]=in.nextline();//輸入資訊
}
for(int i=1;i<=50;i++){
System.out.println(String[i-1]);
}
}
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zonghezhishi/4y3kng.html