java随机点名程序
界面部分
public class demo extends Application {
public void start (Stage primaryStage) {
BorderPane pane=new BorderPane();
//背景图片
ImageView imageView1=new ImageView(new Image("lucky.jpg"));
imageView1.setFitHeight(400);
imageView1.setFitWidth(390);
pane.getChildren().add(imageView1);
pane.setRight(getVBox());
Scene scene = new Scene(pane,500,400);
primaryStage.setResizable(false);
primaryStage.setTitle("随机点名程序 ( by ************ )");
primaryStage.setScene(scene);
primaryStage.show();
}
private VBox getVBox() {
VBox vBox=new VBox(15);
vBox.setPadding(new Insets(100,10,15,15));
vBox.setAlignment(Pos.TOP_RIGHT);
Text text1=new Text("Name");
vBox.getChildren().add(text1);
text1.setFill(Color.BLUE);
text1.setFont(Font.font("Verdana", FontWeight.BOLD, 40));
Button btAdd=new Button("开始点名");
vBox.getChildren().add(btAdd);
btAdd.setOnAction(e -> {
String Name = null;
try {
Name = GetLuckyName();
} catch (Exception e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
text1.setText(Name);
});
VBox.setMargin(btAdd, new Insets(100,0,0,45));
return vBox;
}
随机点名函数
public static String GetLuckyName() throws Exception {
int i,j;
int a=1;int b=107;//根据名单中有多少人数定义的,自行修改,下面的108也是
class Student{
String name;
}
Student[] stu=new Student[108];
java.io.File file=new java.io.File("stu.txt");
Scanner input = new Scanner(file);
for (i=0;i<108;i++) {
stu[i]=new Student();
stu[i].name=input.next();}
j=a+(int)(Math.random()*b);
input.close();
return stu[j].name;
}