Saturday 20 August 2016

Chapter 7 Exercise 1, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*7.1 (Assign grades) Write a program that reads student scores, gets the best score, and then assigns grades based on the following scheme:
Grade is A if score is Ú best - 10
Grade is B if score is Ú best - 20;
Grade is C if score is Ú best - 30;
Grade is D if score is Ú best - 40;
Grade is F otherwise.

The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores, and concludes by displaying the grades. Here is a sample run:

Enter the number of students: 4
Enter 4 scores: 40 55 70 58
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B



import java.util.Scanner;
 
 
public class ProgrammingEx7_1 {
 
 public static void main(String[] args) {
  System.out.print("Enter the number of students:");
  Scanner input = new Scanner(System.in);
  int n = input.nextInt();
  int[] score = new int[n];
  int bestScore = 0;
 
  System.out.print("Enter " + n + " scores:");
 
  for (int i = 0; i < score.length; i++) {
   score[i] = input.nextInt();
   if (bestScore < score[i]) {
    bestScore = score[i];
   }
  }
 
  // Processing grades
 
  char grade;
  for (int i = 0; i < score.length; i++) {
   if (score[i] >= bestScore - 10)
    grade = 'A';
   else if (score[i] >= bestScore - 20)
    grade = 'B';
   else if (score[i] >= bestScore - 30)
    grade = 'C';
   else if (score[i] >= bestScore - 40)
    grade = 'D';
   else
    grade = 'F';
    
   System.out.println("Student " + i +" score is " + score[i] +" and grade is " + grade);
  }
 
 }
 
}

No comments :

Post a Comment