java實現點名,並記錄被點次數

來源:互聯網
上載者:User

java實現點名,並記錄被點次數

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.PrintStream;import java.util.ArrayList;import java.util.Scanner;public class Demoe1 {public static void main(String[] args)throws Exception {File f=new File("src/t7/names.txt");//用於存放讀取進來的學生姓名ArrayList<String> nameList=new ArrayList<String>();//定義用於讀取檔案的輸入資料流Scanner cin=new Scanner(new FileInputStream(f));cin.useDelimiter("\n");//讀取學生姓名到集合中while(cin.hasNext()){nameList.add(cin.next());}cin.close();//關閉輸入資料流f.delete();//刪除原有的文字檔//用於儲存處理後的學生姓名ArrayList<String> resultList=new ArrayList<String>();for(String s:nameList){//用於去掉斷行符號換行s=s.substring(0,s.length()-1);//把處理後的學生姓名加來新的容器中resultList.add(s+"\t"+0);}//定義檔案輸出資料流PrintStream ps=new PrintStream(new FileOutputStream(f));//把處理之後的學生姓名寫到文字檔中for(String s:resultList)ps.println(s);ps.close();//關閉輸出資料流}}

學生姓名和時間

public class Student { private String name; private int time; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; }}

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.PrintStream;import java.util.ArrayList;import java.util.Random;import java.util.Scanner;public class RollCall { public static void main(String[] args) throws Exception { File f = new File("src/t8/names.txt"); Scanner cin = new Scanner(new FileInputStream(f)); cin.useDelimiter("\n"); // 用於儲存學生姓名和被點次數 ArrayList<Student> sList = new ArrayList<Student>(); while (cin.hasNext()) { String s = cin.next(); // 用空格把學生姓名和次數他隔開 String[] ss = s.split("\\s+");// 正則 Student stu = new Student(); stu.setName(ss[0]); // 把字串類型的次數轉換成int類型的次數 // 並存入Student類的對象中 stu.setTime(Integer.parseInt(ss[1])); sList.add(stu); } cin.close(); f.delete(); // 產生隨機下標 Random r = new Random(); // 下標的上界為數組的長度 int i = r.nextInt(sList.size()); Student student = sList.get(i); System.out.println(student.getName()); // 被點過名的學生,點名次數加1 sList.get(i).setTime(student.getTime() + 1); writeStudent(f,sList); } public static void writeStudent(File f, ArrayList<Student> sList)throws Exception { // 建立檔案輸出資料流 PrintStream ps = new PrintStream(new FileOutputStream(f)); for (Student s : sList) { // 把學生姓名和被點次數拼成字串 String st = s.getName() + "\t" + s.getTime(); // 把學生資訊輸出到文字檔中 ps.println(st); } ps.close(); }}    
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.