Advertisement
grioool

Untitled

Oct 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package OAiP;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11.  
  12. public class Program2 {
  13.  
  14. public static void main(String[] args) throws FileNotFoundException {
  15. saveMatrixToFile("output.txt");
  16. int[][] studentMarks = loadMatrixFromFile("student_marks.txt");
  17.  
  18. int[] negativeStudentMarks = new int[30];
  19. for (int i = 0; i < 30; i++) {
  20. negativeStudentMarks[i] = countNegativeMarks(studentMarks[i]);
  21. }
  22.  
  23. for (int i = 0; i < 30; i++) {
  24. System.out.print(String.format("Студент N%2d:", (i + 1)));
  25. for (int j = 0; j < 10; ++j) {
  26. System.out.print(String.format("%3d", studentMarks[i][j]));
  27. }
  28. System.out.println(String.format(" : негативных оценок = %d", negativeStudentMarks[i]));
  29. }
  30. }
  31.  
  32. private static void saveMatrixToFile(String path) {
  33. Random random = new Random();
  34. try {
  35. BufferedWriter output = new BufferedWriter(new FileWriter(path));
  36.  
  37. for (int i = 0; i < 30; ++i) {
  38. for (int j = 0; j < 10; ++j) {
  39. int mark = random.nextInt(10) + 1;
  40. output.write(String.format("%3d", mark));
  41. }
  42. output.newLine();
  43. }
  44.  
  45. output.flush();
  46. output.close();
  47. } catch (IOException e) {
  48. System.out.println("Ошибка ввода/вывода!");
  49. }
  50. }
  51.  
  52. private static int[][] loadMatrixFromFile(String path) throws FileNotFoundException {
  53. Scanner input = new Scanner(new File(path));
  54.  
  55. int[][] result = new int[30][10];
  56.  
  57. for (int i = 0; i < 30; ++i) {
  58. for (int j = 0; j < 10; ++j) {
  59. if (input.hasNextInt()) {
  60. result[i][j] = input.nextInt();
  61. }
  62. }
  63. }
  64.  
  65. input.close();
  66.  
  67. return result;
  68. }
  69.  
  70. private static int countNegativeMarks(int[] marks) {
  71. int count = 0;
  72.  
  73. for (int i = 0; i < 10; i++) {
  74. if (marks[i] < 4) {
  75. count++;
  76. }
  77. }
  78.  
  79. return count;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement