By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

Array race result input from user

P: 1
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!



Expand|Select|Wrap|Line Numbers
  1.         System.out.println("Please input the number of race entrants: ");
  2.         //request number of race entrants
  3.         int TotalEntrants = input.nextInt();
  4.         // allows user to enter number of runners
  5.  
  6.         Runner[] RaceEntrants = new Runner[TotalEntrants];
  7.         //declares and instantiates Runner array
  8.  
  9.         for (int i = 0; i < RaceEntrants.length; i++)
  10.         {
  11. int x;
  12.             //declaring int for entrants number as array starts on 0
  13.             x=i+1;
  14.             //adding 1 to x on every loop
  15.             System.out.print("Please enter race entrant #" + x + "'s name? ");
  16.             String name = input.next();
  17.             //Gathering name
  18.             System.out.print("Please enter race entrant #" + x +  "'s time? ");
  19.             double time = input.nextDouble();
  20.             //Gathering time
  21.             RaceEntrants[i] = new Runner(name, time);
  22.             //RaceEntrants array will hold a name and time for every runner
  23.         }
Dec 11 '19 #1
Share this Question
Share on Google+
1 Reply


dev7060
Expert 100+
P: 162
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.
Dec 11 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.