• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Beginner  RSS feed

 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to make a program that stores and analyses game statistics for 6 players and provide the user with the high ,lows and averages. I am having trouble with getting the average of the numbers entered and displaying the name of the person with the highest and lowest score. Any help would be appreciated


 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That is the loop you use to do the average calculation.
That says:

For all the scores in the array
If the score equals the total then set the total to the score.
Set average to be the total divided by the number of scores.

What is step 2 doing?
 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea i understand what you're saying, what if I changed it to something like


 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I have figured it out. Now I have to do is display the name of the person with the highest and lowest score.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a problem, I believe, in the for loop that takes in 6 different names. Each name that you are inputting overrides the last.

Also, how will you know what user got the highest or lowest score? There's no assignment in the for loops that sets a value to a user.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you meant it like that since you are immediately printing out each name. But I believe an array would make it better in order to retrieve the correct user's name associated with the correct score.
 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any advice on what to do?
 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you help me out on the code, I am very confused
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what user gets the highest and what user gets the lowest? Do you have that information before hand or is it just random?
 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I have the code set up is you enter the 6 names then enter 6 numbers. I need to get the numbers and names (inputted by user) to correspond to print the highest score with the name and lowest score with the name.
 
Daniel Andres
Ranch Hand
Posts: 94
3
AngularJS C++ Chrome Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot help with the code since it is better when someone explain to us what may be wrong and we can then code it. We learn more this way (also, those are the rules) Lol

Take a look at the for loop that takes in the names. You can also store them in an array and perhaps you could even use Math.random to assign the highest to a lucky user.
Knowing the index of the highest then, you could perhaps assign that the user at that index to the variable Highest that you created.

 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You do not need an array to store all those values. Even less do you need two arrays. You shou‍ld consider a Score class with name and score fields. You can simply go through all the scores, and whenever you find a larger or smaller score, record the score and the name. There is actually no need to record any other information; for calculating averages, you only need a running total, and for max/min, you only need a record of the previous max/min (with or without name).

Now, let't look at your code. Ouch! Not only have you tried to squeeze so much into the main method that it is too long, but you have also formatted it so badly there could be all sorts of logical errors hiding there which nobody can see. We have some suggestions about formatting. Also get yourself a decent text editor, not M$ NotePad. That can help you with automatic indentation, bracket matching, etc. If I ever use Windows® I like NotePad++ which you can download free of charge.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a couple of suggestions...

1) have you proven that your arrays are populated correctly?  it's often helpful to create some utility methods - things you write that won't actually be used in your final program, but help you develop it.  So write a method that prints out the contents of an array.  call it on each array you populate and PROVE to yourself they are correct.

2) I'd strongly suggest you refactor that code so everything isn't in the main method.  You are always better served by writing many small methods, rather than one monolithic one.  This allows you to keep the different pieces separate and distinct. once you know your "getInput" method is right, you can use it over and over and not worry about it any more.

all that aside...

if you want to write code to find the name of the user with the highest score. step 1 is to forget about Java, and just write out in English how you'd do it.  pretend you are talking to a young child. Give her step-by-step directions on how to do it.  Until you can explain what you are trying to do in English (or whatever), you can't write it in Java.
 
Ted O Brien
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I changed around the main body of the code just to see if you guys think it is better.




 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted O Brien wrote:Ok I changed around the main body of the code just to see if you guys think it is better.


I don't think that it is a matter of us "think[ing] it is better". Does it do what you want done? If so, great. If not, what is wrong with it? And how do you think you can fix that?

So... does the code do what you want done? Is the behavior correct?

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!