Answer:
import java.util.Scanner;
public class Linkify {
public static void main(String[] args) {
int[][] records = {
{1232, 10, 23, 45, 56},
{2343, 45, 43, 24, 78},
{2343, 34, 45, 45, 45},
{3423, 67, 65, 65, 56}
};
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("MENU");
System.out.println("1. View all students' records");
System.out.println("2. View a student's records by ID");
System.out.print("Please enter your choice: ");
choice = input.nextInt();
switch(choice) {
case 1:
System.out.println("| StudentID | Quiz1 | Quiz2 | Mid-Term | Final |");
for(int i = 0; i < records.length; i++) %-5d
break;
case 2:
System.out.print("Enter a student ID: ");
int id = input.nextInt();
boolean found = false;
for(int i = 0; i < records.length; i++) {
if(records[i][0] == id)
System.out.printf("
}
if(!found) {
System.out.println("Invalid ID, please try again.");
}
break;
default:
System.out.println("Invalid choice, please try again.");
break;
}
System.out.println();
} while(choice != 1 && choice != 2);
input.close();
}
}
Step-by-step explanation:
This program first initializes a 2D array called records with the student records data. It then displays a menu with two choices: 1) view all student records, or 2) view a specific student's record by ID.
The program uses a do-while loop to keep displaying the menu and accepting input until the user chooses either option 1 or option 2. Inside the switch statement, the program either loops through the entire records array to print all student records or prompts the user to enter a student ID and searches the records array for a matching ID to print the corresponding record.
The printf method is used to format the output into columns with fixed widths. If an invalid choice or ID is entered, an error message is displayed and the menu is displayed again. Once the user chooses either option 1 or option 2, the program exits.