Results 1 to 2 of 2

Thread: min max

  1. #1
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default min max

    i'm really close at getting this but when i tried entering the scores of 60 80 70, the 2nd highest would be 60 instead of 70

    Java Code:
    import java.util.Scanner;
    	public class Testing {
    		public static void main(String[] args) {
    				
    			Scanner input = new Scanner(System.in);
    
    			int max1 = Integer.MIN_VALUE;
    			int max2 = Integer.MIN_VALUE;
    			int number, numberOfStudents;
    			String name = "";
    			String name1 = "";
    			String name2 = "";
    				
    			System.out.print("Enter the number of students: ");
    			numberOfStudents = input.nextInt();
    				
    			if(numberOfStudents < 2) {
    				System.out.println("You must enter 2 or more students.");
    				System.exit(0);
    			}
    				
    			for (int i = 1; i <= numberOfStudents; i++) {
    				System.out.print("Enter the student #" + i + "'s first name: ");
    				name = input.next();
    					
    				System.out.print("Enter " + name + "'s score: ");
    				number = input.nextInt();
    					
    				if (number == -1 || number == 101){
    					System.out.println("Please enter a valid score.");
    					System.exit(0);
    				}
    				else if (number > max1) {
    					//This moves the previous high score to max2 and name2
    					max2 = max1;
    					name2 = name1;
    						
    					//Makes max1 and name1 as the current highscore
    					max1 = number;
    					name1 = name;
    				}
    			}
    			System.out.println(name1 + " has the top score of " + max1 + ".");
    			System.out.println(name2 + " has the second highest score of " + max2 + ".");
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: min max

    You need another else if( ... ) clause: a number may be smaller than max1 but larger than max2 (as in your example).

    kind regarrds,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •