Pages

Friday, 12 August 2016

Chapter 5 Exercise 8, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

(Find the highest score) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name of the student with the highest score.



import java.util.Scanner;
 
public class ProgrammingEx5_8 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter the number of students:");
  int n = input.nextInt();
  int max = 0;
  String maxName = "";
 
  for (int i = 0; i < n; i++) {
   System.out.print("Enter name and score of student (" + (i + 1)
     + "):");
   String name = input.next();
   int score = input.nextInt();
   if (score > max) {
    max = score;
    maxName = name;
   }
 
  }
 
  System.out.println("The student with the highest score is " + maxName);
  System.out.println("The highest score is " + max);
 
 }
 
}

No comments :

Post a Comment