Я делаю программу, которая будет распечатываться на основе того, кто победит. (Подробнее о задаче в ссылке) https://open.kattis.com/problems/vote
У меня проблемы с распечаткой «нет победителя» с правильным значением. Я пытаюсь распечатать «нет победителя», только когда все элементы в моем arrayList одинаковы. ех.
arr = [10, 10, 10]
затем распечатайте:
"no winner"
Как проверить, все ли элементы в arrayList равны?
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class B {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
for (int i = 0; i < testCase; i++ ) {
int candidate = sc.nextInt();
int maximum = 0;
int winner = 0;
boolean tie = false;
ArrayList<Integer> votes = new ArrayList<>();
for (int j = 0; j < candidate; j++) {
int nVotes = sc.nextInt();
votes.add(nVotes);
}
int imax = votes.indexOf(Collections.max(votes));
int max = Collections.max(votes); // max winner
int in = votes.indexOf(votes);
int index = imax + 1;
int sum = 0;
for(int o = 0; o < votes.size(); o++)
{
sum = sum + votes.get(o);
if (votes.get(o) == votes.get(o) ) {
tie = true;
}
}
if (max > (sum / 2)) {
System.out.println("majority winner " + index);
}
else if (max < (sum / 2)) {
System.out.println("minority winner " + index);
}
else if (votes.get(i) == votes.get(i)) {
System.out.println("no winner");
}
}
}
}