【问题标题】:Getting two separate arrays out of going through a void method通过 void 方法获得两个单独的数组
【发布时间】:2014-03-30 01:37:40
【问题描述】:

我正在参加 Java 的 100 级课程...非常新,这是我的第一个问题,请多多包涵:)。

我有一个任务,其中两个用户要输入他们的姓名,然后获取他们玩过的游戏数,然后获取他们玩过的每场游戏的分数,将分数放入一个数组(或两个数组???) ,然后比较两个数组和分数,看看谁赢得了每场比赛,或者他们是否并列,然后显示结果。我不相信我不必对其进行排序或搜索。

我的老师说获取分数必须是一个 void 方法,但我不确定如何将这个 void 方法中的值获取到每个玩家的数组中,以便我可以比较分数值。

这是我将分数放入数组的方法:

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

这是我的主要方法:

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

是的,这有点乱,但我正在尝试自己解决这个问题。这是我的 compareScores 方法,但我知道这很疯狂。我已经尝试过解决它,但是是的,它变得越来越困难。

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

是的,我知道这很多,而且我不擅长解释,但如果有人可以帮助我,我不介意尝试解释更多正在发生的事情,如果这意味着获得帮助,因为我真的被困得疯了现在。

感谢所有帮助/)

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/2806545/…
  • 关于一般的 void 方法,请阅读“通过引用传递”。如果将对象的引用作为参数传递给方法,则不必返回任何内容,因为您已经拥有对实际对象的引用,并且可以更改对象的值。我认为在你的情况下,你正在接受分数,你可以在你的主要方法中做到这一点,它是 void 类型。
  • 谢谢!我的老师是这么说的。所以,我会继续努力,对参考传递做更多的研究(我现在对方法还是很陌生),看看我能解决什么问题。 @prash

标签: java arrays methods


【解决方案1】:

如果这是一项课堂作业,我建议您与您的同学或高年级同学交谈,如果允许的话,这始终是最好的学习方式。 如果不允许,那么您也不应该在这里为您的作业寻求帮助:-)

上学的一部分是学习如何思考、学习语言和解决问题

尝试一些 OOP 或面向对象的原则。

在实际编码之前,一定要在纸上写下你的算法。使用 sudo 代码,列出所有步骤,然后寻找缩短它的方法。我们都知道硬件很便宜,但一个优雅的解决方案也必须是一个有效的解决方案。不要忘记 KISS,保持简单 S*** :-) 祝你好运。

【讨论】:

    【解决方案2】:

    这只是向您展示其中一种方法。我正在分享代码,因为我看到您是初学者,并且您努力编写代码。此外,您不能复制粘贴以在代码中使用。下一次,向我们展示你的算法,不要指望我们提供代码。我们只会为您提供指导或向您展示修复它的位置。

    注意:不包括异常处理

    创建一个 Person 类来存储您期望的值,

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

    现在是主类,

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

    【讨论】:

    • 谢谢!我对编码还是很陌生,我会发布我不理解的部分,但我不确定我错过了哪一部分。我和我的老师谈过,他给我看的东西看起来更像你在这里给我看的东西……我会用这两个推动正确的方向,看看我能不能得到它。非常感谢! :D
    • 谢谢,继续……如果您有这种感觉,可以接受作为答案
    【解决方案3】:

    “是的,这有点乱,但我正在尝试自己解决这个问题。这是我的 compareScores 方法,但我知道这太疯狂了。”

    正是……!!!!尝试简化您的代码并清理您的方法。我相信这会很有帮助。 我不确定你的编程背景是什么,但从来没有用以下方式编写过 java 代码:

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

    以级联方式使用函数的返回值是不好的做法。

    同样,一旦您清理了代码,我们就能更轻松地为您提供帮助。

    【讨论】:

      【解决方案4】:

      在 Java 中,数组通过引用 (http://way2java.com/arrays/passing-arrays-to-methods/) 传递。所以你不必返回数组。在 inputScore 方法中分配给数组的所有值都将保留。 我想让您注意我在您的 compareScores 方法中注意到的一些事情。为什么你使用带有索引 j 的内部 FOR 循环,但从未使用过该索引?

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

      您正在比较每个数组的第 i 个元素 j 次,它会打印 j 次相同的结果。确保您正确理解问题。我认为您正在尝试比较每个玩家在相应游戏中的得分。例如,游戏 A 和玩家 B 在游戏 #3 中的得分,如果他们都玩过该游戏。如果是这种情况,您将不需要迭代两个 for 循环。 (只是一个想法)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-23
        • 1970-01-01
        • 2021-08-08
        • 2012-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多