1

I'm in a 100 level class for Java... very new and this is my first question so bear with me please :).

I have an assignment where two users are to enter their names, then get the number of games they played, then get the scores for each game that they played, put the scores in an array (or two arrays???), then compare the two arrays and scores to see who won each game or if they're tied, then display results. I don't have to sort it or search through it I don't believe.

Ss my teacher says getting the scores has to be a void method, but I'm not sure how I get the values from this void method into arrays for each player so I can compare the score values.

Here's my method for getting the scores into an array:

    public static void inputScores(int[] array, String msg)  {
    String inputScores = "";
    for (int i = 1; i < array.length; i++) {
        inputScores = JOptionPane.showInputDialog(msg + " enter your score for game " + i);
        array[i] = Integer.parseInt(inputScores);
    }
}

Here's my main method:

     public static void main(String[] args) {
 //Get the number of games from the user, this is the array length
int[] numberOfGamesPlayed = new int[getPositiveIntOrQuit("How many games were played?")];

 //Get the names of the players (Two)
String input, input2 = "";
input = JOptionPane.showInputDialog("Player 1 - enter your name");
inputScores(numberOfGamesPlayed, input);


input2 = JOptionPane.showInputDialog("Player 2 - enter your name");
inputScores(numberOfGamesPlayed, input2);

 //Get two separate arrays, one for Player1, one for Player2





 String output = "";

 showResults(WINDOW_TITLE, output);
     }
    }

Yeah, it's a bit messy and such but I'm trying to figure this out on my own kinda. Here's my compareScores method, but I know it's crazyyyyy messed up. I've tried working through it but yeah, it's getting difficult.

    public static String compareScores(int[] playerOne, int[] playerTwo, String msg, String msg2) {
    String output = "";
    int player1Wins = 0;
    int player2Wins = 0;
    int tie         = 0;
    for(int i = 0; i < playerOne.length; i++) {
        for(int j = 0; j < playerTwo.length; j++); {
            if(playerOne[i] > playerTwo[i]) {
                output += "Game " + i + " results:" + "\n" + msg + " outscored " + msg2
                        + " by " + (playerOne[i] - playerTwo[i]) + "points.";
                player1Wins++;
            }
            else if(playerOne[i] < playerTwo[i]) {
                player2Wins++;
                output += "Game " + i + " results:" + "\n" + msg2 + " outscored " + msg
                        + " by " + (playerTwo[i] - playerOne[i]) + "points.";
            }
            else {
                output += msg + " and " + msg2 + " scored the same number of points.";
                tie++;
            }

    }
        output += "Summary: ";


        }
    return output;
}

Yeah I know it's a lot and I'm not good at explaining but if anyone can help me at all I don't mind trying to explain more of what's going on if it means getting help because I am really stuck like crazy right now.

Thanks for any and all help /)

  • this might help: http://stackoverflow.com/questions/2806545/does-java-have-something-like-cs-ref-and-out-keywords – attila Mar 30 '14 at 01:44
  • Regarding having a void method in general, please read on 'pass by reference'. If you pass a reference of an object as parameter to a method, then you dont have to return anything, as you already have the reference to the actual object and you can change the values of the object. I think in your case, you are accepting the scores, you can do it in your main method which is void type. – spiderman Mar 30 '14 at 02:13
  • Thank you! That's what my teacher said. So, I will work on that, do some more research on the pass by references (I'm still really new to methods right now) and see what I can work out. @prash – user3477088 Mar 30 '14 at 14:40

4 Answers4

2

If this is a class assignment I will recommend that you talk to your classmate or upper classmates, that is always the best way to learn, if that is allowed. If is not allowed, then you should not be asking for help to your assignment here either :-)

Part of going to school is to learn how to think, learn the language and problem solve.

Try some OOP or object oriented principles.

Always write your algorithm in paper before actually coding it. Use sudo code, list all steps and then look for ways to make it shorter. We all know that hardware is cheap, but an elegant solution has to be also an efficient solution. And don't forget about KISS, Keep It Simple S*** :-) Good luck.

1

"Yeah, it's a bit messy and such but I'm trying to figure this out on my own kinda. Here's my compareScores method, but I know it's crazyyyyy messed up."

Exactly...!!!! Try simplifying your code and cleaning up your methods. I'm sure it will help a lot. I'm not sure what is your programming background but never ever code java in the following manner:

int[] numberOfGamesPlayed = new int[getPositiveIntOrQuit("How many games were played?")];

It is bad practice to use functions' return values in a cascaded manner.

Again, once you clean up your code, we are able to help you more easily.

Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31
1

In Java, arrays pass by reference (http://way2java.com/arrays/passing-arrays-to-methods/). So you don't have to return the array. All the values assigned to the array inside the inputScore method, will remain. I'd like to bring your attention to something that I noticed in your compareScores method. Why do you use an inner FOR loop with index j, but never used that index?

if(playerOne[i] > playerTwo[i]) {...

You are comparing the i-th element of each array j times which prints the same result j times. Make sure that you understand the problem correctly. I think that you are trying compare score of each player for the corresponding game. For example, the score of play A and player B for the game #3, if they both have played that game. If that's the case, you won't need to iterate two for loops. (Just a thought)

Watz
  • 829
  • 8
  • 11
1

This is just to show you one of the approaches. I am sharing the code as I see you are a beginner and you made efforts to code it. Also, you cannot copy-paste to use in your code. Next time, show us your algorithm, and don't expect the code from us. We will only guide you or show you where to fix it.

Note: Exception handling is not covered

Create a Person class to store the values that you expect,

public class Person {
    String name;
    int games;
    int[] scores;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGames() {
        return games;
    }

    public void setGames(int games) {
        this.games = games;
    }

    public int[] getScores() {
        return scores;
    }

    public void setScores(int[] scores) {
        this.scores = scores;
    }
}

Now the main class,

public class Scores {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new InputStreamReader(System.in));
        int scores[];
        System.out
                .println("**************************************************");
        System.out.println("First player");
        Person player1 = new Person();
        System.out.println("Enter name");
        player1.setName(scanner.nextLine());
        System.out.println("Enter the number of games played");
        player1.setGames(scanner.nextInt());
        scores = new int[player1.getGames()];
        for (int i = 0; i < player1.getGames(); i++) {
            System.out.println("Enter the score for each game");
            scores[i] = scanner.nextInt();
        }
        player1.setScores(scores);

        System.out
                .println("**************************************************");
        scanner = new Scanner(new InputStreamReader(System.in));
        System.out.println("Second player");
        Person player2 = new Person();
        System.out.println("Enter name");
        player2.setName(scanner.nextLine());
        System.out.println("Enter the number of games played");
        player2.setGames(scanner.nextInt());
        scores = new int[player2.getGames()];
        for (int i = 0; i < player2.getGames(); i++) {
            System.out.println("Enter the score for each game");
            scores[i] = scanner.nextInt();
        }
        player2.setScores(scores);

        // compare the scores and output the result

        compareScores(player1, player2);

    }

    private static void compareScores(Person player1, Person player2) {
        // assuming both of them played the same number of games.
        if (player1.getGames() == player2.getGames()) {
            int player1Scores[] = player1.getScores();
            int player2Scores[] = player2.getScores();
            for (int i = 0; i < player1.getGames(); i++) {
                System.out.println("Game " + (i + 1) + " Results");
                if (player1Scores[i] > player2Scores[i]) {
                    System.out.println(player1.getName()+" won ");
                } else if (player1Scores[i] < player2Scores[i]) {
                    System.out.println(player2.getName()+" won ");
                } else {
                    System.out.println("Scores are level");
                }
            }
        }
    }
}
spiderman
  • 10,892
  • 12
  • 50
  • 84
  • Thank you! I am still very new to coding, I would have just posted the bit I didn't understand but I wasn't really sure of which part I was missing out on. I talked to my teacher and he showed me what looks to be a lot more like what you're showing me here... I will use these two pushes in the right direction to see if I can get it. Thank you very much! :D – user3477088 Mar 30 '14 at 14:38
  • Thank you, keep going..you can accept as answer if you feel that way – spiderman Mar 30 '14 at 15:53