Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OAiP;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Random;
- import java.util.Scanner;
- public class Program2 {
- public static void main(String[] args) throws FileNotFoundException {
- saveMatrixToFile("output.txt");
- int[][] studentMarks = loadMatrixFromFile("student_marks.txt");
- int[] negativeStudentMarks = new int[30];
- for (int i = 0; i < 30; i++) {
- negativeStudentMarks[i] = countNegativeMarks(studentMarks[i]);
- }
- for (int i = 0; i < 30; i++) {
- System.out.print(String.format("Студент N%2d:", (i + 1)));
- for (int j = 0; j < 10; ++j) {
- System.out.print(String.format("%3d", studentMarks[i][j]));
- }
- System.out.println(String.format(" : негативных оценок = %d", negativeStudentMarks[i]));
- }
- }
- private static void saveMatrixToFile(String path) {
- Random random = new Random();
- try {
- BufferedWriter output = new BufferedWriter(new FileWriter(path));
- for (int i = 0; i < 30; ++i) {
- for (int j = 0; j < 10; ++j) {
- int mark = random.nextInt(10) + 1;
- output.write(String.format("%3d", mark));
- }
- output.newLine();
- }
- output.flush();
- output.close();
- } catch (IOException e) {
- System.out.println("Ошибка ввода/вывода!");
- }
- }
- private static int[][] loadMatrixFromFile(String path) throws FileNotFoundException {
- Scanner input = new Scanner(new File(path));
- int[][] result = new int[30][10];
- for (int i = 0; i < 30; ++i) {
- for (int j = 0; j < 10; ++j) {
- if (input.hasNextInt()) {
- result[i][j] = input.nextInt();
- }
- }
- }
- input.close();
- return result;
- }
- private static int countNegativeMarks(int[] marks) {
- int count = 0;
- for (int i = 0; i < 10; i++) {
- if (marks[i] < 4) {
- count++;
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement