-4

I currently have java homework that I would appreciate some help with. We are to calculate a team record scenario.
We are given the following numbers:
Team1 Points {23,45,65,20}

Opponent Points {20,30,20,18}

I threw these into an array. I also created a public boolean. Basically, you are to pull these points from the array to the boolean? And let the boolean decide which team won? Obviously team1 has won, but we are supposed to let the computer decide, not the human.

Here is my code:

public class TeamScore {

public static boolean Winner(int team1Points, int opponentPoints) {
    if (team1Points > opponentPoints) {
        return true;
    } else {
        return false;
    }
}

public static void main(String[] args) {
    // Scanner in = new Scanner(System.in);
    int[] team1Points = { 23, 45, 65, 20 };
    int[] opponentPoints = { 20, 30, 20, 18 };
    int team1 = 1;
    int opponent = 1;

    for (int i = 0; i < 5; i++) {
        if (Winner(team1Points[i], opponentPoints[1])) {
            team1 += 1;
        } else {
            opponent += 1;
        }
    }

    for (int i = 0; i < 5; i++) {
        if (team1 > 0 && opponent == 0) {
            System.out.println("Team 1 has the perfect record!");
        } else {
            System.out.println("Win" + Arrays.toString(team1Points));
            System.out.println("Loss" + Arrays.toString(opponentPoints));
        }
    }   
}

Could anyone possibly help me? I am currently in programming II, but I did not have the best teacher in programming I. Any help would be appreciated!

EDIT:
I do not think this is a duplicate question because I already can fix it by changing the variable i-->1. My problem is that the computer thinks that team1 has already won regardless of the score.

When I run the code I am getting an java.lang.ArrayIndexOutOfBoundsException error. However when I change team1Points[i] to team1Points[1] then it goes okay and tells me that "Team 1 has the perfect record!". However, if I change some of the array values for team1Points to be less than opponentPoints then it still says "Team 1 has the perfect record!".

Dolf
  • 134
  • 9
  • And I would use a good indentation to be more readable. Those brackets are out of places. What is the problem ? – AxelH Jan 25 '17 at 06:28
  • When I run the code I am getting an out of bounds error. However when I change `team1Points[i]` to `team1Points[1]` then it goes okay and tells me that `"Team 1 has the perfect record!"`. However, if I change some of the array values for `team1Points` to be less than `opponentPoints` then it still says `"Team 1 has the perfect record!"`. I am just trying to understand this. It has been kind of hard :/ – Nick Stalf Jan 25 '17 at 06:30
  • `for (int i= 0; i < 5; i++){` will loop from `i=0` to `i=4`, but you have an array of 4 int `[0]` to `[3]` – AxelH Jan 25 '17 at 06:32
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – AxelH Jan 25 '17 at 06:32
  • 2
    as a sidenote, if looping over an array make use of its `array#length` variable, it will will most likely be a big help in your loop condition. Same for the `Collection#size` method when you are about to be using a collection – SomeJavaGuy Jan 25 '17 at 06:34
  • I do not think it is a duplicate because if I change it from i-->1 then the problem goes away. However, I do not know how to fix this problem I am having when the computer thinks that `team1` has the perfect record regardless of the score. – Nick Stalf Jan 25 '17 at 06:40

1 Answers1

0

Not sure why you have a method Winner (also as Kevin said, it should be winner because of naming conventions) which turns ´(a > b)´ into a large if-statement. Similar stuff appear elsewhere in your code.

Your variables ´team1, opponent = 1´ inexplicably start with the value 1, am I to understand this as a way for your code to imply to a reader that both teams initialize at a win? Using 0 would probably make more sense.

Your game ought to crash from an indexoutofboundsexception at ´team1Points[i]´, as you have arrays of length 4, but your loops runs 5 times (the currently used range is [0-4], inclusive). Changing your loops to i=1 won't help, as the issue is that you eventually encounter team1Points[4] due to the i < 5 statement.

I don't know what game you are modelling or how it works, but the comparison ´Winner(team1Points[i],opponentPoints[1])´ looks like a blatant error to me (you always look at the opponents score for their second round).

Why are you printing your results 5 times? If you want to print the first message only when team1 won all rounds and the point for each round otherwise, you would need to use the loops counter as an index to the arrays in second case. First case should break loop so its not written five times, in addition you don't need to check team1>0 && opponent==0 as it's enough to only check if ´opponent==0´ (speaking of this conditional, it only works if you initialize the variables at 0 as I mentioned before). You could have checked if team1 equals size of the array instead, but imo thats more of a hassle than opponent==0.

Lastly, please fix your indenting. use the preview system so you can make double sure before posting.

Edit: Kevin also brings up a good point that you should be using the length of the array in your loops second statement.

felix
  • 93
  • 8
  • Thank you for your reply, I appreciate it. I have tried some of your suggestions but they haven't seemed to change much. The object of the assignment is to have the computer decide who has won from the given numbers. As I said before team 1 has obviously won, but it is for the computer to decide. What would be the best was to go about this. I am not trying to get a direct answer, I would like to learn my mistakes for future reference. – Nick Stalf Jan 25 '17 at 06:54
  • If you are still receiving an `IndexOutOfBoundsException` at `team1Points[i]` then the reason is written in my third paragraph, and a solution has been given by Kevin. In your loops the counter `i` should traverse the interval `[0,array#length)`, as trying to access any index outside of that range will give said Exception. – felix Jan 25 '17 at 07:28
  • Thanks for all the help! I fixed it finally! – Nick Stalf Jan 25 '17 at 15:44