Untitled

mail@pastecode.io avatar
unknown
java
23 days ago
3.0 kB
3
Indexable
Never
import java.util.Scanner;
import java.lang.String;
public class A3_Q2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// -------------------------------------------------- 
		// Assignment {3} 
		// Written by: {Erfan Jahedi, 40224280} 
		// For COMP 248 Section {H} – Fall 2023 
		// --------------------------------------------------
		System.out.println("Welcome to Mont-Halloween Skiing Contest Ranker."
				+ ("\nWhenever a skiier makes it to the finish line, enter their name and skiing time."));
		Scanner input = new Scanner(System.in);
		String[] name = new String[20];
		double[] time = new double[20];
		int firstPodiumIndex = 0;
		int secondPodiumIndex = 0;
		int thirdPodiumIndex= 0;
		int temp;
		for (int i = 0; i < 20; i++) {
			System.out.println("Please input the skier's name: ");
			name[i] = input.next();
			input.nextLine();
			System.out.println("Please input the skier's time: ");
			time[i] = input.nextDouble();
			if (i == 0) {
				System.out.println("First podium goes to " + name[firstPodiumIndex] + " who made it in " + time[firstPodiumIndex]);
			}
			else if (i == 1) {
				secondPodiumIndex = 1;
				if (time[firstPodiumIndex] < time[secondPodiumIndex]) {
					System.out.println("First podium goes to " + name[firstPodiumIndex] + " who made it in " + time[firstPodiumIndex]);
					System.out.println("Second podium goes to " + name[secondPodiumIndex] + " who made it in " + time[secondPodiumIndex]);
				}
				else {
					temp = firstPodiumIndex;
					firstPodiumIndex = secondPodiumIndex;
					secondPodiumIndex = temp;
					System.out.println("First podium goes to " + name[firstPodiumIndex] + " who made it in " + time[firstPodiumIndex]);
					System.out.println("Second podium goes to " + name[secondPodiumIndex] + " who made it in " + time[secondPodiumIndex]);
				}
			}
			else if (i > 1){
				thirdPodiumIndex = i;
				if (time[thirdPodiumIndex] < time[secondPodiumIndex] && time[thirdPodiumIndex] < time[firstPodiumIndex]) {
					temp = secondPodiumIndex;
					secondPodiumIndex = firstPodiumIndex;
					firstPodiumIndex = thirdPodiumIndex;
					thirdPodiumIndex = temp;
				}
				else if (time[thirdPodiumIndex] < time[secondPodiumIndex] && time[thirdPodiumIndex] > time[firstPodiumIndex]) {
					temp = secondPodiumIndex;
					secondPodiumIndex = thirdPodiumIndex;
					thirdPodiumIndex = temp;
				}
				System.out.println("First podium goes to " + name[firstPodiumIndex] + " who made it in " + time[firstPodiumIndex]);
				System.out.println("Second podium goes to " + name[secondPodiumIndex] + " who made it in " + time[secondPodiumIndex]);
				System.out.println("Third podium goes to " + name[thirdPodiumIndex] + " who made it in " + time[thirdPodiumIndex]);
			}
			System.out.println("Do you want to add another skiier? ");
			String answer = input.next();
			if (answer.equals("Y") || answer.equals("yes") || answer.equals("y")) {
					continue;
				}
			else {
				System.out.print("Goodbye!");
				break;
			}
		}
		input.close();
	}

}