我创建了一个简单的游戏,其中 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 );
最佳答案
编辑:只有 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 类来存储差异 + 获胜者
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;
}
}
}
关于java - 没有HashMap可以做到这一点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349187/