Using Nested Control Structures

The following Java program uses Nested Control Structures. Consider the following program that reads students ID’s and their test scores. For each student, the program outputs the students ID, test score, and grade. The program also outputs the number of students in the class.
  1. import java.util.*;
  2.  
  3. public class nestedControlled
  4. {
  5.         static Scanner console = new Scanner(System.in);
  6.         public static void main(String[] args)
  7.         {
  8.                 String studentId;
  9.                 int testScore;
  10.                 int count = 0;
  11.                
  12.                 System.out.print("Enter the student ID: "
  13.                         + "(Enter XXX to end):");
  14.                         System.out.flush();
  15.                         studentId = console.next();
  16.                         System.out.println();
  17.                        
  18.                 while (!studentId.equals("XXX"))
  19.                 {
  20.                         count++;
  21.                        
  22.                         System.out.print("Enter the test score: ");
  23.                         System.out.flush();
  24.                         testScore = console.nextInt();
  25.                         System.out.println();
  26.                        
  27.                         System.out.print("Student Id = " + studentId
  28.                                 + ", test score = "
  29.                                 + testScore + ", and grade = ");
  30.                                
  31.                                 if (testScore >= 90)
  32.                                         System.out.println("A.");
  33.                                 else if (testScore >= 80)
  34.                                         System.out.println("B.");
  35.                                 else if (testScore >= 70)
  36.                                         System.out.println("C.");
  37.                                 else if (testScore >= 60)
  38.                                         System.out.println("D.");
  39.                                        
  40.                                 else
  41.                                         System.out.print("F.");
  42.                         System.out.print("Enter the student ID "
  43.                                 + "(Enter XXX to end): ");
  44.                                 System.out.flush();
  45.                                 studentId = console.next();
  46.                                 System.out.println();
  47.                                
  48.                                        
  49.                 }//end while
  50.                 System.out.println("\nStudents in class = " + count);  
  51.         }
  52. }
Sample Run: Enter the student ID: (Enter XXX to end):IRT040687 Enter the test score: 90 Student Id = IRT040687, test score = 90, and grade = A. Enter the student ID (Enter XXX to end): KLY097865 Enter the test score: 80 Student Id = KLY097865, test score = 80, and grade = B. Enter the student ID (Enter XXX to end): XXX Students in class = 2 The program works as follows: The statements
  1.  String studentId;
  2.                 int testScore;
  3.                 int count = 0;
are the declaratiosn of variables: StudentId, testScore and count. The statement
  1.  System.out.print("Enter the student ID: "
  2.                         + "(Enter XXX to end):");
prompts the user to enter the student’s Id and type XXX to terminate the program. The statement count++; increments the value of count by 1. The statement
  1.  while (!studentId.equals("XXX"))
  2.                 {
  3.                         count++;
  4.                        
  5.                         System.out.print("Enter the test score: ");
  6.                         System.out.flush();
  7.                         testScore = console.nextInt();
  8.                         System.out.println();
  9.                        
  10.                         System.out.print("Student Id = " + studentId
  11.                                 + ", test score = "
  12.                                 + testScore + ", and grade = ");
  13.                                
  14.                                 if (testScore >= 90)
  15.                                         System.out.println("A.");
  16.                                 else if (testScore >= 80)
  17.                                         System.out.println("B.");
  18.                                 else if (testScore >= 70)
  19.                                         System.out.println("C.");
  20.                                 else if (testScore >= 60)
  21.                                         System.out.println("D.");
  22.                                        
  23.                                 else
  24.                                         System.out.print("F.");
  25.                         System.out.print("Enter the student ID "
  26.                                 + "(Enter XXX to end): ");
  27.                                 System.out.flush();
  28.                                 studentId = console.next();
  29.                                 System.out.println();
  30.                                
  31.                                        
  32.                 }//end while
is the while loop of the program. It allows the user to enter the student test score and outputs the corresponding grade. The statement System.out.println("\nStudents in class = " + count); displays the number of students in the class.

Add new comment