In: Computer Science
Create JAVA PROGRAM, and write comment for codes also.
3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks =========================================================================== import java.util.Scanner; public class Race { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // variables needed int total5KSeconds = 0; // total 5k seconds for all races int total10KSeconds = 0; // total 10k seconds for all races int total5KRuns = 0, total10KRuns = 0; // how many 5k and 10k races to calculate average int raceTime = 0; // current race time int choice = 0; // user select 0 to quit 1 to enter 5k race 2 for 10k race times int bestTime5K = 0, bestTime10K = 0; // trakc the best time for 5k and 10k races // keep asking until user enters 0 to quit while (true) { System.out.println("Enter [1] - 5000 km "); System.out.println("Enter [2] - 10,000 km "); System.out.println("Enter [0] - exit "); choice = scanner.nextInt(); if (choice == 1) { System.out.print("Enter time (in seconds) to complete 5,000 km race: "); raceTime = scanner.nextInt(); total5KSeconds += raceTime; total5KRuns += 1; // when the user enters the time for 5K first time update // the best time to the racetime if (total5KRuns == 1) bestTime5K = raceTime; else { //// update the best time to the least time if (bestTime5K > raceTime) bestTime5K = raceTime; } } else if (choice == 2) { System.out.print("Enter time (in seconds) to complete 10,000 km race: "); raceTime = scanner.nextInt(); total10KSeconds += raceTime; total10KRuns += 1; // when the user enters the time for 10K first time update // the best time to the racetime if (total10KRuns == 1) bestTime10K = raceTime; else { // update the best time to the least time if (bestTime10K > raceTime) bestTime10K = raceTime; } } else if (choice == 0) { break; } else { System.out.println("Invalid choice. Please choose either 1 or 2 or 0. Try again!"); } } // prints the best time and average time for both 5k and 10k races if (total5KRuns == 0) { System.out.println("No data for 5,000 km race"); } else { System.out.println("5,000 km race stats - "); System.out.println("Best time : " + (bestTime5K / 60) + " minutes"); System.out.println("Average time : " + (total5KSeconds / (60 * total5KRuns)) + " minutes"); } System.out.println(); if (total10KRuns == 0) { System.out.println("No data for 10,000 km race"); } else { System.out.println("10,000 km race stats - "); System.out.println("Best time : " + (bestTime10K / 60) + " minutes"); System.out.println("Average time : " + (total10KSeconds / (60 * total10KRuns)) + " minutes"); } } }
========================================================================
thank you, please do upvote : )
thanks a lot !!