93.4k views
0 votes
7. Complete the following problem below in java

The program will first display a menu that enables the users to choose whether they
want to view all students 'records or view only the records of a specific student by the
student's id. See sample below.
MENU
1, View all students' records
2. View a student's records by ID
Please enter your choice 1
If the user types in for Choice 1, it displays this:
StudentID | Quiz1 | Quiz2 | Mid-Term | Final |
1232
| 10 | 23
56
2343
| 45
143
124
| 78 |
2343
| 34
145
145
145
13423
| 67 16 165
|56|
Example:
11232
145
If the user types in for Choice 2, it will ask the user to enter in an ID. If the user enters in
an invalid ID, the user has to keep entering one in until a correct one is entered. Once a
valid ID is entered, will display the students ID, Quiz1, Quiz2, Mid-Term, and Final
grade.
| 10 | 23
145
| 56 |

7. Complete the following problem below in java The program will first display a menu-example-1
User Splunk
by
7.7k points

1 Answer

1 vote

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.

User FriskyGrub
by
8.7k points

No related questions found