YWN Coffee Room » Decaffeinated Coffee

Java Problem

(23 posts)
  • Started 2 years ago by OneOfMany
  • Latest reply from OneOfMany

Tags:

No tags yet.

  1. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    I'm in an Intro to programming class, and I'm really having trouble with variable scope and loops. Here's my loop:

    import java.util.Scanner;

    public static void scoreAverages(int players, int games)
    {
    Scanner keyboard = new Scanner(System.in);

    int playerAverage;
    int teamScoreAccumulator = 0;
    int teamScoreAverage;

    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?

    Posted 2 years ago #
  2. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    *last brace left in by accident

    Anyone? I'm going nuts here. :(

    Posted 2 years ago #
  3. yitayningwut
    I have no idea wut this screen name means. Do YOU know what this screen name means?

    Sorry, Java just isn't my cup of tea.

    Posted 2 years ago #
  4. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    Ha! I figured out how to get around it. :D

    Posted 2 years ago #
  5. -™
    dash

    int playerAverage;

    I have a feeling that this line is going to cause problems.

    Posted 2 years ago #
  6. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    yitayningwut: Just got it. :P
    dash: Why? Because it's an int?

    Posted 2 years ago #
  7. 2qwerty
    Member

    I thought you were talking about an Island in Indonesia

    Posted 2 years ago #
  8. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    Kudos. I bet everyone else thinks I'm talking about coffee. ;)

    Posted 2 years ago #
  9. -™
    dash

    Why? Because it's an int?

    Imagine if every baseball player had a batting average of 0.

    Posted 2 years ago #
  10. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    Good point. But this is for bowling. It makes more sense to use int.

    Posted 2 years ago #
  11. ItcheSrulik
    Formerly college sheigetz. Now ger.

    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?

    Posted 2 years ago #
  12. 2qwerty
    Member

    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
    int max=0;
    int highestPlayer;

    //then right after you know playerAverage do this
    if(playerAverage>max)
    {
    max = playerAverage;
    highestPlayer=x;
    }

    //right before writting out team average add this
    System.out.println("Player with the highest average: " + highestPlayer);

    Posted 2 years ago #
  13. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    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. :)

    Posted 2 years ago #
  14. ItcheSrulik
    Formerly college sheigetz. Now ger.

    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.

    Posted 2 years ago #
  15. 2qwerty
    Member

    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.

    Posted 2 years ago #
  16. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    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.

    Posted 2 years ago #
  17. HaLeiVi
    Plays the aeolian harp by air

    I found JavaScript to be a great introduction to the concept of programming. It doesn't have to compile for you to see results. It doesn't drive you nuts about what type your variable is or was, which leaves out a large obsticle for a beginner.

    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.

    Posted 2 years ago #
  18. 2qwerty
    Member

    For lowest avg I guess you can change if statement to
    playerAverage<max

    And change in the beginning
    int max=100; // which is the Max number it can be

    Posted 2 years ago #
  19. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    ItcheSrulik: Yeah, we're doing both. :)

    HaLeiVi: We aren't learning JavaScript. But I just wish my Computer Science department would realize that they're supposed to be teaching Computer Science, not just utilitarian computer programming.

    2qwerty: That's exactly why they're teaching us Java. But what about more fundamental programming, y'know?

    Posted 2 years ago #
  20. Another name
    Member

    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.

    Posted 2 years ago #
  21. cpno
    Member

    @OneOfMany I'm interested in perhaps going back to school and finish up a computer science degree I once started. You say your father is an engineer, so maybe you can tell me are there many jobs available or has outsourcing taken away most of them? I live in Brooklyn.

    Posted 2 years ago #
  22. Another name
    Member

    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.

    Posted 2 years ago #
  23. OneOfMany
    Today, the Impressively Arbitrary Nymphadora the Purple is sporting One Of her Many eyebrow colors, as well as her Morgul-blade ^_^

    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.

    Posted 2 years ago #

RSS feed for this topic

Reply

You must log in to post.