scanner与list的结合:简易学生签到系统


1
package cn.help.student; 2 3 4 import javax.swing.*; 5 import java.awt.*; 6 import java.io.*; 7 import java.util.ArrayList; 8 import java.util.List; 9 import java.util.Scanner; 10 11 12 public class Student { 13 List<String> student = new ArrayList<>(); 14 15 public Student() { 16 Scanner sc = new Scanner(System.in); 17 File file = new File("D://student.txt"); 18 19 JFrame frame = new JFrame("学生添加系统");//创建窗体 20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方式 21 frame.setSize(500, 500);//设置窗体大小 22 frame.setLayout(null);//设置窗体布局方式 23 JTextArea textArea = new JTextArea();//创建文本域 24 textArea.setBounds(75, 80, 340, 500); 25 textArea.setFont(new Font("微软雅黑", 0, 18));//设置文字靠上 frame.add(textArea);//加入文本域 26 frame.setVisible(true);//设置窗体可视 27 28 try { 29 FileWriter fileWriter = new FileWriter(file); 30 System.out.println("输入0开始添加,输入1退出"); 31 String begin = sc.next();//用于储存决定是否开始 32 if (begin.equals("0")) { 33 String temp; 34 do { 35 System.out.println("继续添加,输入1退出"); 36 temp = sc.next();//用于储存输入的字符串 37 if (!temp.equals("1")) { 38 student.add(temp); 39 fileWriter.write(temp + "\n"); 40 textArea.append(temp + "\n");//在文本域后面继续添加字符 41 fileWriter.flush(); 42 } 43 } while (!temp.equals("1")); 44 System.out.println("检测到1,已退出程序"); 45 } 46 System.exit(1); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 52 public static void main(String[] args) { 53 new Student(); 54 } 55 }

 

posted @ 2020-02-25 11:12  abalone  阅读(337)  评论(0编辑  收藏  举报