Java Problem

Home Forums Decaffeinated Coffee Java Problem

Viewing 23 posts - 1 through 23 (of 23 total)
  • Author
    Posts
  • #601788
    OneOfMany
    Participant

    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?

    #846524
    OneOfMany
    Participant

    *last brace left in by accident

    Anyone? I’m going nuts here. 🙁

    #846525
    yitayningwut
    Participant

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

    #846526
    OneOfMany
    Participant

    Ha! I figured out how to get around it. 😀

    #846527
    dash™
    Participant

    int playerAverage;

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

    #846528
    OneOfMany
    Participant

    yitayningwut: Just got it. 😛

    dash: Why? Because it’s an int?

    #846529
    2qwerty
    Participant

    I thought you were talking about an Island in Indonesia

    #846530
    OneOfMany
    Participant

    Kudos. I bet everyone else thinks I’m talking about coffee. 😉

    #846531
    dash™
    Participant

    Why? Because it’s an int?

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

    #846532
    OneOfMany
    Participant

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

    #846533
    ItcheSrulik
    Member

    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?

    #846534
    2qwerty
    Participant

    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);

    #846535
    OneOfMany
    Participant

    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. 🙂

    #846536
    ItcheSrulik
    Member

    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.

    #846537
    2qwerty
    Participant

    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.

    #846538
    OneOfMany
    Participant

    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.

    #846539
    HaLeiVi
    Participant

    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.

    #846540
    2qwerty
    Participant

    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

    #846541
    OneOfMany
    Participant

    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?

    #846542
    Another name
    Participant

    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.

    #846543
    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.

    #846544
    Another name
    Participant

    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.

    #846545
    OneOfMany
    Participant

    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.

Viewing 23 posts - 1 through 23 (of 23 total)
  • You must be logged in to reply to this topic.