January 26, 2012 at 12:15 am #601788
I’m in an Intro to programming class, and I’m really having trouble with variable scope and loops. Here’s my loop:
public static void scoreAverages(int players, int games)
Scanner keyboard = new Scanner(System.in);
int teamScoreAccumulator = 0;
for(int x = 1; players >= x; x++)
int scoreAccumulator = 0;
for(int y = 1; games >= y; y++)
System.out.println(“Enter score ” + y +” for player ” + x + “: “);
scoreAccumulator = scoreAccumulator + keyboard.nextInt();
teamScoreAccumulator = teamScoreAccumulator + scoreAccumulator;
playerAverage = scoreAccumulator / games;
System.out.println(“Player ” + x + ” average: ” + playerAverage);
teamScoreAverage = teamScoreAccumulator / (games * players);
System.out.println(“Team average: ” + teamScoreAverage);
I need to get it to somehow store the individual averages outside of the loop, because I have to determine who has the highest average. Any tips?
January 26, 2012 at 1:51 am #846524
*last brace left in by accident
Anyone? I’m going nuts here. 🙁
January 26, 2012 at 2:25 am #846525
Sorry, Java just isn’t my cup of tea.
January 26, 2012 at 3:40 am #846526
Ha! I figured out how to get around it. 😀
January 26, 2012 at 3:54 am #846527
I have a feeling that this line is going to cause problems.
January 26, 2012 at 4:02 am #846528
yitayningwut: Just got it. 😛
dash: Why? Because it’s an int?
January 26, 2012 at 4:17 am #846529
I thought you were talking about an Island in Indonesia
January 26, 2012 at 4:23 am #846530
Kudos. I bet everyone else thinks I’m talking about coffee. 😉
January 26, 2012 at 5:19 am #846531
Why? Because it’s an int?
Imagine if every baseball player had a batting average of 0.
January 26, 2012 at 5:33 am #846532
Good point. But this is for bowling. It makes more sense to use int.
January 26, 2012 at 3:37 pm #846533
You want your loop to store each player’s average in an array. Then you want to call max to find the highest average.
Here are a couple comments on your program.
1- Where are your comments? Right now you aren’t dealing with production code so you don’t see how important documentation is yet, but it’s a good habit to get into and it makes it easier for people to understand your code when you need help.
2- Why do you create a new scanner on every method call?
3- Why are you dividing by the number of players times the number of games?
BTW, which school teaches Java as an intro language?
January 26, 2012 at 5:33 pm #846534
I was joking about the island i wanted to see how many people heard of it before. Anyway your answer should be something like this:
//in the begining add
//then right after you know playerAverage do this
max = playerAverage;
//right before writting out team average add this
System.out.println(“Player with the highest average: ” + highestPlayer);
January 26, 2012 at 6:01 pm #846535
Lol, that’s really my problem. I looked at a bunch of help sites online and they’re all telling me to use an array. But we didn’t learn arrays yet. The point of this whole exercise was really just to introduce us to methods, so I’m not even sure why my teacher made the loop stuff so complicated.
Anyway, I figured out what I needed to do without extrapolating the variables. But I’m always stumped when it comes to getting input in a loop and then getting it out. It this even going to be relevant later on?
To answer your questions:
1- I usually stick comments in at the end. We haven’t written anything intense enough to need extensive commenting. You’re right, though – next time I ask for help I think I’ll put in some comments.
2- As opposed to globalizing it? My teacher doesn’t like that.
3- It’s for an overall team average. So I need to divide all accumulated scores by the amount of games played by each player.
Yeah, it’s not such a great program, especially for my concentration. I’m probably going to transfer a little further down the line. My father works in computer engineering, and he was also very skeptical of learning Java first. He decided that I should really learn Perl, so he’s going to teach me that over the summer. 🙂
January 26, 2012 at 7:09 pm #846536
You’ll get to memory management eventually. Then you’ll understand (or be expected to understand :)) what should be at which scope. Your father is right about perl first, though if you didn’t have anyone teaching you I would recommend python instead. Easier.
January 26, 2012 at 7:10 pm #846537
I guess you still didnt see my answer which doesnt use arrays.
And by the way Perl is great but no one wants to use it anymore so i would suggest PHP over Perl.
January 26, 2012 at 7:26 pm #846538
2qwerty: Java, Sumatra..sure. 😉
That’s takah more or less what I figured out. For some reason, I thought I needed the lowest average also. I couldn’t figure out how to do that. I still can’t, but it doesn’t matter now.
January 26, 2012 at 8:38 pm #846539
The best way to learn anything is to be able to see results. When you learn to play a musical instrument it is also a good idea to learn some songs well early on. This way, instead of only practicing for a year, you get to hear yourself play the instrument.
January 26, 2012 at 8:48 pm #846540
For lowest avg I guess you can change if statement to
And change in the beginning
int max=100; // which is the Max number it can be
January 26, 2012 at 9:45 pm #846541
ItcheSrulik: Yeah, we’re doing both. 🙂
2qwerty: That’s exactly why they’re teaching us Java. But what about more fundamental programming, y’know?
January 26, 2012 at 10:16 pm #846542
OneofMany, regarding programming programs (as you do not seem content with your current schooling), many have informed me of the quality of Touro. I know many coffeeroomers have scoffed at the value of a Touro degree, but their computer major is respected in the secular world. In addition, Touro faculty works hard to find their students successful jobs. I personally, know of many of their alumni that currently have prestigious jobs in upper class firms.
January 26, 2012 at 10:23 pm #846543
January 26, 2012 at 10:36 pm #846544
cpno, there are still many, many computer jobs available. In fact, even more now than there were years ago, thanks to great advances in technology. Although, part-times jobs are still limitted.
January 27, 2012 at 12:07 am #846545
Another name: If I transfer, it’s definitely going to be an engineering school. Just so you know, I don’t think Touro is so bad. I don’t think it’s what I want, though.
cpno: My father has a different type of job than the ones that got outsourced; but anyway, I think outsourcing is going out of style.
- You must be logged in to reply to this topic.