Question

In: Computer Science

Can you provide java code for a program that prompts the user by asking "How many...

Can you provide java code for a program that prompts the user by asking "How many one mile races have you ran?". After the user inputs how many one mile races have they run it then prompts the user to input how many seconds it took for them to finish each race. So for example, if the user ran 6 races then the user will have to input 6 race times. Is there a way when you prompt the user to enter the race time to ask "How many seconds did you take you to finish race 1?" and the same for the second race (if they completed more than one race and so on for how many races they completed. Then can it display the average number of seconds it took for every race, by saying "Your average race time is" + averageRacetime+ "in seconds". Then can the program ask if the user "Do you want to start a new average race time?" if no then the program starts if yes then the program starts over and again asks "How many one mile races have you ran?" following the same structure as the first one.

Solutions

Expert Solution

Code:

import java.util.*;
class averageRace
{
   public static void main(String[] args)
   {
       while(true)
       {
           int n;
           double averageRacetime,sum=0;
           /*Decalred averiables*/
           Scanner scnr=new Scanner(System.in);
           /*Creting scanner object*/
           System.out.print("How many one mile races have you ran? ");
           n=scnr.nextInt();
           /*Rading no of races */
           double[] time = new double[n];
           for(int i=0;i<n;i++)
           {
               System.out.print("How many seconds did you take you to finish race "+(i+1)+"?" );
               time[i]=scnr.nextDouble();
               /*Reading time in each race*/
               sum+=time[i];
               /*Adding the time to sum*/
           }
           averageRacetime=sum/(double)n;
           /*Calculating average race time*/
           System.out.println("Your average race time is " + averageRacetime+ " seconds");
           System.out.print("Do you want to start a new average race time?");
           choice=scnr.next();
           /*Asking user to continue or not*/
           if(choice.equals("yes"))
           {
               continue;
           }
           else
           {
               break;
           }
       }
   }
}

Output:

Indentation:


Related Solutions

JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to enter two integers and an operation and then outputs the answer. Prompt the user three times, don't expect all the inputs on one line. Possible operations are + - * / No other words or symbols should be output, just the answer. Requested files import java.util.Scanner; /** * This is a simple calculator Java program. * It can be used to calculate some simple...
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT