【问题标题】:Can this be done without a HashMap?这可以在没有 HashMap 的情况下完成吗?
【发布时间】:2019-12-16 00:07:56
【问题描述】:

我创建了一个简单的游戏,其中 2 名玩家输入输入,领先较大的一方在游戏结束时被宣布为获胜者以及获胜的利润。您输入的第一个输入是每个玩家获得的回合数。在输出中,我需要提到的玩家编号和边距。我能够完成它,但我想知道这是否可以在不使用 HashMap 的情况下完成,因为我也已经在使用数组列表。有人可以建议这样做的方法吗?

    Scanner o = new Scanner(System.in);
    List<Integer> arrlist = new ArrayList<Integer>();
    int q = o.nextInt(), a1, b1, diff, max, w;
    HashMap<Integer, Integer> map = new HashMap<>();

    for (int i = 0; i < q; i++) {   // q is the number of turns
        a1 = o.nextInt();   //first player input
        b1 = o.nextInt();   //second player input
        diff = (a1 > b1) ? a1 - b1 : b1 - a1;   //getting the difference
        w = (a1 > b1) ? 1 : 2;   //picking the winner for the turn
        map.put(diff, w);
        arrlist.add(diff);
    }

    max = arrlist.get(0); 
    for (int j = 0; j < arrlist.size(); j++) { 
        if (arrlist.get(j) > max)
            max = arrlist.get(j); 
    } 
    System.out.printf("%d %d", map.get(max), max );  

【问题讨论】:

  • 您可以使用运行总计 - 有两个变量,每个用户一个。
  • 或者创建一个新的class,它代表一个“差异”和一个“赢家”。 class Result { private int diff; private int winner; }。然后你可以只跟踪其中一个,如果新差异大于旧差异,则更新局部变量。
  • 只是为了让你自己和其他人在未来更容易:如果你给你的变量取有用的名字,你可以摆脱大部分的 cmets!只需将q 重命名为numberOfTurns 并将a1 重命名为inputPlayer1 并将b1 重命名为inputPlayer2。并且调用 ArrayList arrlist 是毫无意义的:命名它以表明它意味着什么。

标签: java arraylist hashmap


【解决方案1】:

已编辑:只有 1 类解决方案:

public static void main(String[] args) {
    Scanner o = new Scanner(System.in);
    System.out.print("Input max match: ");
    int q = o.nextInt();
    int maxDiff = 0;
    int maxWinner = 0;

    for (int i = 0; i < q; i++) {   // q is the number of turns
        System.out.println("Match " + String.valueOf(i + 1));
        System.out.print("1st player: ");
        int player1Input = o.nextInt();   //first player input
        System.out.print("2st player: ");
        int player2Input = o.nextInt();   //second player input
        int diff = Math.abs(player1Input - player2Input);   //getting the difference
        int winner = (player1Input > player2Input) ? 1 : 2;   //picking the winner for the turn

        if(diff > maxDiff) {
            maxDiff = diff;
            maxWinner = winner;
        }
    }

    if(maxWinner > 0) {
        System.out.printf("Winner player: %d Max diff: %d", maxWinner, maxDiff);
    }
}  

旧解决方案:添加 Match 类来存储 diff + 获胜者

public class StackOverflow_59349187 {

    public static void main(String[] args) {
        Scanner o = new Scanner(System.in);
        System.out.print("Input max match: ");
        int q = o.nextInt();

        List<Match> matches = new ArrayList<>();

        for (int i = 0; i < q; i++) {   // q is the number of turns
            System.out.println("Match " + String.valueOf(i + 1));
            System.out.print("1st player: ");
            int player1Input = o.nextInt();   //first player input
            System.out.print("2st player: ");
            int player2Input = o.nextInt();   //second player input
            int diff = Math.abs(player1Input - player2Input);   //getting the difference
            int winner = (player1Input > player2Input) ? 1 : 2;   //picking the winner for the turn
            Match match = new Match(winner, diff);
            matches.add(match);
        }

        if(matches.size() > 0) {
            Match maxDiffMatch = matches.get(0);
            for (int j = 0; j < matches.size(); j++) {
                Match currentMatch = matches.get(j);
                int currentDiff = currentMatch.getDiff();
                int maxDiff = maxDiffMatch.getDiff();
                if (currentDiff > maxDiff) {
                    maxDiffMatch = currentMatch;
                }
            }
            System.out.printf("Winner player: %d Max diff: %d", maxDiffMatch.getPlayer(), maxDiffMatch.getDiff());
        }
    }

    static class Match {
        private final int player;
        private final int diff;

        public Match(int player, int diff) {
            this.player = player;
            this.diff = diff;
        }


        public int getPlayer() {
            return player;
        }

        public int getDiff() {
            return diff;
        }
    }
}

【讨论】:

  • 嗨,这个很好,但我想只在一个类中运行它,而不必创建对象。谢谢
  • @wakanada 我只编辑了1个类的解决方案,看看,希望对你有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 2014-07-04
  • 1970-01-01
  • 2011-07-08
  • 2017-10-30
相关资源
最近更新 更多