В настоящее время я работаю над проектом java, и я должен сделать программу, которая генерирует число от 1000 до 2000, а затем дает вам возможность ввести количество игроков, все имена и догадки в двух разных массивах, затем создайте два метода: один, чтобы найти самое близкое предположение к фактическим числам, а другое - сообщить или найти победителя, совпадающего с их предположением и распечатать его. Im возникли проблемы с моим вторым способом, им рисует пустую на логической части прямо сейчас это мой код и вывод:Java Guessing Game Project (начинающий)
public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
ArrayList tempList = new ArrayList();
ArrayList tempList1 = new ArrayList();
int counter = 0;
int temp = 0;
for(int index = 0; index < array.length; index++)
{
temp = answer - array[index];
temp = Math.abs(temp);
if(temp == winner)
{
counter++;
tempList.add(array1[index]);
tempList1.add(array[index]);
}
}
for(int index = 0; index < tempList.size(); index++)
{
if(tempList.size() == 1);
{
System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is "
+ winner + " away from the actual number of jelly beans.");
if(tempList.size() > 1)
{
System.out.println("The winners are: ");
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is "
+ winner + " away from the actual number of jelly beans.");
}
}
if(tempList.size() == 1 && winner == 0)
{
System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is "
+ "the exact number of jelly beans in the jar.");
if(tempList.size() > 1)
{
System.out.println("The winners are: ");
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is "
+ "the exact number of jelly beans in the jar.");
}
}
}
}
Это выход он производит, когда есть больше чем один победитель.
There were 1532 jelly beans in the jar.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are:
Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are:
greg, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are:
Jim, with a guess of 1200 which is 332 away from the actual number of jelly beans.
Это то, что он должен выглядеть
There were 1500 jelly beans in the jar.
The winners are:
Mike with a guess of 1475, which is 25 away from the actual number of jelly beans.
Tony with a guess of 1525, which is 25 away from the actual number of jelly beans.
Im, имеющих проблемы с частью метода я знаю, что это много, но если вам нужно, чтобы увидеть остальную часть кода, дайте мне знать, что любая помощь будет оценил, спасибо. (Мне кажется, мне нужен способ аннулирования первой распечатки, если счетчик проходит один, но я не уверен, как это сделать)
Удалить эту точку с запятой с конца этого line: 'if (tempList.size() == 1);'. – rgettman
удалил его и теперь нет выхода –
Никогда, никогда не используйте необработанные типы! Просто сидение. –