Task 1: Bar Rotator

Write a complete computer program that rotates the bar (denoted using “0”s) in the matrix. The program should
1. ask a user for an input of a rotation degree (0, 45, 90, 135, 180, 225, 270, 315, 360),
2. rotate the bar clockwise,
3. print the rotated bar in the matrix to the screen,use an empty space to separate elements.
4. wait for the user to input “exit” (ignore case) to terminate the program.
5. test your program using the following inputs:
Example Input:
90
45
Example Output:

1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1
1 1 1 1 1 0 0 1 1 1 1 1

1 0 1 1 1 1 1 1 1 1 1 1
0 1 0 1 1 1 1 1 1 1 1 1
1 0 1 0 1 1 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 1 1 1
1 1 1 0 1 0 1 1 1 1 1 1
1 1 1 1 0 1 0 1 1 1 1 1
1 1 1 1 1 0 1 0 1 1 1 1
1 1 1 1 1 1 0 1 0 1 1 1
1 1 1 1 1 1 1 0 1 0 1 1
1 1 1 1 1 1 1 1 0 1 0 1
1 1 1 1 1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 1 1 1 0 1

//package lab9;

import java.util.*;

public class task1 {
    public static void main(String[] args) {

        int[][] array = new int[12][12];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = 1;
            }
        }
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String input = in.next();
            String m = input.toLowerCase();
            if (m.equals("exit")) {
                break;
            }
            if (m.equals("0") || m.equals("180") || m.equals("360")) {
                for (int i = 5; i <= 6; i++) {
                    for (int j = 0; j < 12; j++) {
                        array[i][j] = 0;
                    }
                }
            }
            if (m.equals("90") || m.equals("270")) {
                for (int i = 0; i < 12; i++) {
                    for (int j = 5; j <= 6; j++) {
                        array[i][j] = 0;
                    }
                }
            }
            if (m.equals("45") || m.equals("225")) {
                int j = 1;
                for (int i = 0; i < 12 && j < 12; i++) {
                    array[i][j] = 0;
                    j++;
                }
                int b=0;
                for (int i = 1; i < 12 && b < 11; i++) {
                    array[i][b] = 0;
                    b++;
                }
            }

            if (m.equals("135") || m.equals("315")) {
                int j = 10;
                for (int i = 0; i <12 && j>=0; i++) {
                    array[i][j] = 0;
                    j--;
                }
                int b=11;
                for (int i = 1; i <12 && b>=0; i++) {
                    array[i][b] = 0;
                    b--;
                }
            }

            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    System.out.print(" " + array[i][j]);
                }
                System.out.println();
            }
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    array[i][j]=1;
                }
            }
        }
    }
}

Task 2: Record Sanity Checker

At the end of each semester, students’ scores are required to input into the college record system for archiving. In some colleges, these scores are manually inputted into the computer system. Before the students’ scores are archived for long term storage, the system often perform some sanity check. In the appended file (StudentScore.txt), it contains 210 student records. Each record contains a student number and a score. A student number must starts with “T00”, followed by 6 digits; a score must be in the range [0, 100], inclusively.
Write a complete computer program to check each student record, whether the student’s score is in the valid range and/or whether the student number is in the correct format, then print the line(s) containing illegal record(s) to the screen.

//package lab9;

import java.io.*;
import java.util.*;
import java.io.FileNotFoundException;;

public class lin {
    public static void main(String[] args) {
        File score = new File("StudentScore.txt");
        try {
            Scanner in = new Scanner(score);
            in.nextLine();

            while (in.hasNextLine()) {
                String line = in.nextLine();
                Scanner lineScanner = new Scanner(line);
                while (lineScanner.hasNext()) {
                    String name = lineScanner.next();
                    String num = "";
                    try {
                        num = lineScanner.next();
                    } catch (Exception e) {
                        System.out.println(name);
//                      lineScanner.close();
                        continue;
                    }
                    if (name.length() != 9||name.charAt(1)!='0'||name.charAt(2)!='0') {
                        System.out.println(line);
                    }
                    try {
                        int h = Integer.parseInt(num);
                        if (h >= 0 && h <= 100) {
                        } else {
                            System.out.println(line);
                        }
                    } catch (Exception e) {
                        System.out.println(line);
                    }
                }
                lineScanner.close();
            }
            in.close();
        } catch (FileNotFoundException e) {

        }

    }

}
最后修改日期:2019年12月24日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。