Advertisement
huyhung94

[KStudy] Code for love

Nov 17th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package com.hungdh;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.util.Random;
  11.  
  12. public class CodeForLove {
  13.  
  14.     /**
  15.      *
  16.      * @param path
  17.      * @return mảng 10 phẩn tử, mỗi phần tử chứ số lần xuất hiện
  18.      *
  19.      * VD: điểm 1: 5 lần, điểm 2: 10 lần, ... điểm 10: 9 lần,
  20.      * arrResult: {5, 10, ..., 9};
  21.      */
  22.     public static int[] readFile(String path) throws IOException {
  23.         FileReader fr = new FileReader(new File(path));
  24.         BufferedReader br = new BufferedReader(fr);
  25.         int[] arrResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  26.         String num = "";
  27.         while ((num = br.readLine()) != null) {     // Đọc từng dòng, cho đến khi nào hết file
  28.             try {
  29.                 int point = Integer.parseInt(num);  // Ép "chữ số" đọc được về số
  30.                 arrResult[point - 1] += 1;          // Tăng số lần xuất hiện tương ứng với điểm khảo sát
  31.             } catch (NumberFormatException nfe) {
  32.                 nfe.printStackTrace();
  33.             }
  34.         }
  35.         return arrResult;
  36.     }
  37.  
  38.    
  39.     /**
  40.      * Method: ghi chuỗi s xuống file có đường dẫn path
  41.      * @param path
  42.      * @param s
  43.      *
  44.      */
  45.     public static void writeFile(String path, String s) throws IOException {
  46.         FileWriter fw = new FileWriter(new File(path));
  47.         BufferedWriter bw = new BufferedWriter(fw);
  48.         bw.write(s);
  49.         bw.close();
  50.         fw.close();
  51.     }
  52.  
  53.     /**
  54.      * Tạo mảng 100 phần tử, mỗi phần tử được sinh ngẫu nhiên từ 1- 10
  55.      * random.nextInt(10): sẽ trả về giá trị ngẫu nhiên từ 0 - 9;
  56.      */
  57.     public static int[] Survey() {
  58.         Random random = new Random();
  59.         int[] arrSurvey = new int[100];
  60.         for (int i = 0; i < 100; i++) {
  61.             arrSurvey[i] = random.nextInt(10) + 1;
  62.         }
  63.         return arrSurvey;
  64.     }
  65.  
  66.     /**
  67.      * Method tính tỉ lệ khảo sát
  68.      * @param path: đường dẫn file thứ 2
  69.      * @param data: dữ liệu đã khảo sát
  70.      */
  71.     public static void calculateSurvey(String path, int[] data)
  72.             throws IOException {
  73.         StringBuffer s = new StringBuffer();
  74.         for (int i = 0; i < data.length; i++) {
  75.             s.append((i + 1) + "\t" + (float) data[i] / 100 + " %\n");
  76.         }
  77.         writeFile(path, s.toString());
  78.     }
  79.  
  80.     public static void main(String[] args) {
  81.  
  82.         String file_1 = "Survey.txt";
  83.         String file_2 = "Result.txt";
  84.  
  85.         int[] arrSurvey = Survey();
  86.         System.out.println("Survey success");
  87.         try {
  88.  
  89.             StringBuffer s = new StringBuffer();
  90.             for (int i = 0; i < arrSurvey.length; i++) {
  91.                 s.append(arrSurvey[i] + "\n");
  92.             }
  93.             writeFile(file_1, s.toString());
  94.             System.out
  95.                     .println("Survey data has been written to the file Survey.txt ");
  96.  
  97.             int[] arrResult = readFile(file_1);
  98.  
  99.             calculateSurvey(file_2, arrResult);
  100.             System.out
  101.                     .println("Results of the survey has been written to the file Result.txt ");
  102.  
  103.         } catch (IOException e) {
  104.             // TODO Auto-generated catch block
  105.             e.printStackTrace();
  106.         }
  107.  
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement