Array race result input from user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sophia195
    New Member
    • Dec 2019
    • 1

    #1

    Array race result input from user

    Hi guys I have been asked to program; Allow the user to enter a number of race results (A minimum of two participants can run in a race and a maximum of 7).

    Process the results by
    • Allowing a user to enter a race participants name
    • Allowing a user to enter a race participants completion time (a value between 0.00 &
    10.00 mins.)

    Is the following code sufficient? or should I go another way about it, any help will be beneficial only started java 3 months ago!



    Code:
    		System.out.println("Please input the number of race entrants: ");
    		//request number of race entrants
    		int TotalEntrants = input.nextInt();
    		// allows user to enter number of runners
    
    		Runner[] RaceEntrants = new Runner[TotalEntrants];
    		//declares and instantiates Runner array
    
    		for (int i = 0; i < RaceEntrants.length; i++)
    		{
    int x;
    			//declaring int for entrants number as array starts on 0
    			x=i+1;
    			//adding 1 to x on every loop
    			System.out.print("Please enter race entrant #" + x + "'s name? ");
    			String name = input.next();
    			//Gathering name
    			System.out.print("Please enter race entrant #" + x +  "'s time? ");
    			double time = input.nextDouble();
    			//Gathering time
    			RaceEntrants[i] = new Runner(name, time);
    			//RaceEntrants array will hold a name and time for every runner
    		}
    Last edited by gits; Dec 11 '19, 02:36 PM. Reason: added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    A minimum of two participants can run in a race and a maximum of 7
    a value between 0.00 &
    10.00 mins.
    The code has no condition checks for these.

    - The posted code is incomplete. There is no Runner class here.

    - Declarations are done inside the loop body. It means new variables are declared in each iteration. Is this necessary?

    - Plus, there exist many errors in the code.

    Comment

    Working...