随机点名小程序怎么做

随机点名小程序怎么做,第1张

在使用ExcelExcel制作随机点名小程序这个问题。

软件:Exce2016

电脑:华为MateBook14

系统:Windows10

1、打开文件,进入【开发工具】点击【visual basic】。

2、打开需要点名的所在sheet表,并输入代码,并单击【保存】。

3、d出另存为,设置保存类型,将文件保存为xlsm。

4、在【开发工具】-【插入】一个按钮。

5、在d出的“指定宏,选择sheet”点名,并点击【确定】。

6、更改控件上的名称“点名,并设置基本格式”,后点击【即可】。

import 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("点名")

setDefaultCloseOperation(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<String>list = new ArrayList<String>()

}

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

#define STU_NUM_MAX 64 // 假设最多有64个学生

struct Student  

{

char name[10]

int  stuID

}stu[STU_NUM_MAX]

int exist[STU_NUM_MAX] // 用以保存被点过名

static int index=0 // 记住点名的次数 

void Iitialize(){

for(int i=0i<STU_NUM_MAXi++) exist[i]=0

}

bool IsExist(int id){

for(int i=0i<STU_NUM_MAXi++)

if(exist[i]==id) return true //已存在

return false // 不存在

}

void Add() // 添加数据

{

FILE *fp

int stu_num

printf("\t\t You want to input the number of student?:")

scanf("%d",&stu_num)

for (int i=0i<stu_numi++){

printf("\n")

printf("\t\tPlease input student ID:")

    scanf("%d",&stu[i].stuID)

printf("\t\tPlease input student name:")

scanf("%s",stu[i].name)

fflush(stdin)

}

if((fp=fopen("stu.dat","ab"))==NULL)  {

printf("Can't open file\n")

exit(1)

}

for(int j=0j<stu_numj++)

{   

if(fwrite(&stu[j],sizeof(struct Student),1,fp)!=1) 

printf("Error writing file.\n")

}

   fclose(fp) 

}

void rollcall() // 随机点名

{

FILE *fp

    if((fp=fopen("stu.dat","rb"))==NULL)

{

printf("Can't open file.\n")

exit(1)

}

srand((unsigned)time(NULL))

int i=0

int randID=rand()%(64-1+1)+1 // 1~64

printf("\t\t随机点到的学号为:%d\n\t\t%s\t%s\n",randID,"StuID","StuName") 

do

{

        fseek(fp,i*sizeof(struct Student),SEEK_SET) 

if(fread(&stu[i],sizeof(struct Student),1,fp)) 

{

if(stu[i].stuID==randID&&!IsExist(randID)){

   printf("\t\t%4d\t%5s\n",stu[i].stuID,stu[i].name)

               exist[index++]=randID

   break}

}

  i++

}while(!feof(fp))

fclose(fp)

}

int main()

{

int select=0

char answer='y'

Iitialize()

do 

{

printf("1.添加数据 2.随机点伍没败名 3.退出\n请选择:")

fflush(stdin)

scanf("%d",&select)

switch(select)

{

case 1:

Add()

break

case 2:

rollcall()

break

case 3:

 return 0

}

fflush(stdin)

printf("You want to continue?:")

scanf("%c",&answer)

} while (answer=='y'||answer=='Y')

return 0

}

上面的代码,腔颤我留下几个细节问题留给你自己学着解决,都是很简单的:察逗

上面的代码,我没有对重复的学号作判断。

上面的代码,我没有把点名存放到另一个文件,而是用数组替代(可以实现的也很简单)。我怕写得代码太多,百度限制提交。

上面的代码,是测试数据,stu.dat目标文件并没有64个学生,我只写入了12条数据。

上面的代码,我没有对数据数量(最多64条)作判断。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12535940.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存