Working on this project from my intro java class, heres the gist of what it needs to do --
In the land of Puzzlevania, Aaron, Bob, and Charlie had anargument over
which one of them was the greatest puzzler of all time. To endthe
argumentonce and for all, they agreed on a duel to the death. Aaronis a poor
shooter and only hits his target with a probability of 1/3. Bob isa bit better
and hits his target with a probability of 1/2. Charlie is an expertmarksman
and never misses. A hit means a kill and the person hit drops outof the duel.
To compensate for the inequities in their marksmanship skills, itis decided
that the contestants would fire in turns starting with Aaron,followed by Bob,
and then by Charlie. The cycle would repeat until there was oneman
standing. And that man would be remembered as the greatest puzzlerof all
time.
a. Write a function to simulate a single shot. It should usethe
following declaration:
voidshoot(bool& targetAlive, double accuracy, int&num_alive);
This would simulate someone shooting at targetAlive with thegiven
accuracy by generating a random number between 0 and 1. If therandom
number is less than accuracy, then the target is hit andtargetAlive should
be set to false. Appendix 4 illustrates how to generate randomnumbers.
For example, if Bob is shooting at Charlie, this could be invokedas:
shoot(charlieAlive,0.5, num_alive);
Here, charlieAlive is a Boolean variable that indicates if Charlieis alive. Test
your function using a driver program before moving on to stepb.
b. An obvious strategy is for each man to shoot at the most
accurate shooter still alive on the grounds that this shooter is the
deadliest and has the best chance of hitting back. Write a second
function named start Duel that uses the shoot function to simulate
an entire duel using this strategy. It should loop until only one
contestant is left, invoking the shoot function with the proper target
and probability of hitting the target according to who is shooting.
The function should return a variable that indicates who won the
duel.
c. In your main function, invoke the startDuel function 1,000 timesin
a loop, keeping track of how many times each contestant wins.
Output the probability that each contestant will win wheneveryone
uses the strategy of shooting at the most accurate shooter left
alive.
d. A counter intuitive strategy is for Aaron to intentionally misson his
first shot. Thereafter, everyone uses the strategy of shooting atthe
most accurate shooter left alive. This strategy means that Aaron is
guaranteed to live past the first round, since Bob and Charlie will
fire at each other. Modify the program to accommodate this new
strategy and output the probability of winning for each contestant.
So here is the code I'm working with... So far it goes through only once, but I'm not sure where I should put the loop? Also any other pointers in code would be much appreciated.
import java.util.Random;
public class Duelist
{
Random rnd = new Random();
static int aaron_wins,bob_wins,charlie_wins;
class Shooter
{
public static final int StartingHits = 1;
private String name;
private double accuracy;
private int hitsLeft = StartingHits;
public Shooter(String name, double accuracy)
{
this.name = name;
this.accuracy = accuracy;
}
public String getName() { return this.name; }
public double getAccuracy() { return this.accuracy; }
public boolean isAlive() { return this.hitsLeft > 0; }
public void takeHit() { this.hitsLeft--; }
public void shoot(Shooter target)
{
if (rnd.nextDouble() <= this.getAccuracy())
{
System.out.println(this.getName() + " hits " + target.getName());
target.takeHit();
if (!target.isAlive())
{
System.out.println(target.getName() + " dies.");
}
else
{
System.out.println(this.getName() + " misses " + target.getName());
}
}
}
}
private Shooter [] shooters;
public Duelist()
{
this.shooters = new Shooter []
{
new Shooter("Aaron", 0.33),
new Shooter("Bob", 0.5),
new Shooter("Charlie", 1)
};
}
public Shooter pickTarget(Shooter shooter) {
Shooter victim = null;
for(Shooter possibleVictim : this.shooters) {
if (!possibleVictim.isAlive()) { continue; }
if (shooter==possibleVictim) { continue; }
if (victim == null || possibleVictim.getAccuracy() > victim.getAccuracy()) {
victim = possibleVictim;
}
}
return victim;
}
public void fireAway() {
int currentShooter = 0;
int maxShooter = this.shooters.length;
while(true) {
Shooter shooter = this.shooters[currentShooter++];
if (shooter.isAlive()) {
Shooter victim = pickTarget(shooter);
if (victim!=null) {
shooter.shoot(victim);
} else {
System.out.println(shooter.getName() + " wins.");
if(shooter.getName().equals("Aaron"))
aaron_wins++;
else if(shooter.getName().equals("Bob"))
bob_wins++;
else if(shooter.getName().equals("Charlie"))
charlie_wins++;
break;
}
}
if (!(currentShooter<maxShooter)) { currentShooter=0; }
}
}
public static String beginDuel_alternative_strategy()
{
boolean aaronAlive = true;
boolean bobAlive = true;
boolean charlieAlive = true;
int num_alive = 3;
aaron_wins=bob_wins=charlie_wins=0;
String winner = "";
int round = 1;
do
{
if (aaronAlive)
{
if (round == 1)
{
if (charlieAlive)
shoot(charlieAlive, 1/3.0, num_alive);
else if (bobAlive)
shoot(bobAlive, 1/3.0, num_alive);
}
}
if (bobAlive)
{
if (charlieAlive)
shoot(charlieAlive, 0.5, num_alive);
else if (aaronAlive)
shoot(aaronAlive, 0.5, num_alive);
}
if(charlieAlive)
{
if (bobAlive)
shoot(bobAlive, 1.0, num_alive);
else if (aaronAlive)
shoot(aaronAlive, 1.0, num_alive);
}
round++;
num_alive--;
}while(num_alive > 1);
if (aaronAlive)
{
winner = "Aaron";
aaron_wins++;
}
else if(bobAlive)
{
winner = "Bob";
bob_wins++;
}
else
{
winner = "Charlie";
charlie_wins++;
}
return winner;
}
public static void shoot(boolean targetAlive, double accuracy, int number_alive)
{
Random rnd2 = new Random();
if (rnd2.nextDouble()< accuracy)
{
targetAlive = false;
number_alive--;
}
}
public static void main(String[] args)
{
Duelist duel = new Duelist();
duel.fireAway();
System.out.println("Using first strategy: \n");
System.out.println("Aaron won " + aaron_wins + " duels or " + aaron_wins * 100 + "%\n");
System.out.println("Bob has " + bob_wins + " duels or " + bob_wins * 100 + "%\n");
System.out.println("Charlie has " + charlie_wins + " duels or " + charlie_wins * 100 + "%\n");
System.out.println();
System.out.println("Using alternate strategy: \n");
System.out.println("Winner :" + beginDuel_alternative_strategy());
System.out.println();
System.out.println("Aaron has " + aaron_wins + " duels or " + aaron_wins * 100 + "%\n");
System.out.println("Bob won " + bob_wins + " duels or " + bob_wins * 100 + "%\n");
System.out.println("Charlie won " + charlie_wins + " duels or " + charlie_wins * 100 + "%\n");
}
}
The answer of your question is written in the requirements for your problem:
c. In your main function, invoke the startDuel function 1,000 timesin a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.
// main() pseudocode:
Shooter[] shooters = new Shooter[3](); // or however java syntax is ...
// Set the strategy, name for each of the 3 shooters...
shooters[0].setName("..");
shooters[0].setStrategy(...);
// ...
// get some storage to count which player wins a round...
int[] winCounters = new int[3];
for( int i = 0; i < 1000; i++ )
{
int winner = startDuel(shooters); // returns index of winner...
winCounters[winner]++;
}
// ... output the statistics ....
Related
Currently learning Java as a hobby and am making small game projects to reinforce the concepts. So for this one, I've made a method that creates an array of objects, in this case, a "Computer" object. I'm doing this because I want the user to decide at startup how many computer opponents they want to play against, instead of hard coding a set number of them. Now I want to assign and retrieve a value for each Computer object. For example a Computer name, bet amount, and a dice roll guess.
public class Computer {
static int bet;
static int guess;
int cash;
static Computer[] c;
public static void create(int numComps) {
c = new Computer[numComps];
for (int i = 0; i < numComps; i++) {
c[i] = new Computer();
c[i].cash = Game.startCash;
c[i].bet = bet();
c[i].guess = guess();
c[i].display();
}
}
public static int bet() {
bet = Rng.rand(Game.startCash / 50) * 50;
return bet;
}
public static int guess() {
guess = Dice.roll();
return guess;
}
public void display() {
String name = "Computer ";
System.out.println("My name is " + name + " i bet " + bet + " and guess " + guess);
}
}
When i do Computer.create(5) i get
My name is Computer i bet 150 and guess 9
My name is Computer i bet 50 and guess 3
My name is Computer i bet 450 and guess 11
My name is Computer i bet 250 and guess 11
My name is Computer i bet 50 and guess 10
This output gives the appearance of working but i don't think i'm on the right track. For the name i want the syntax to be something like, name = "Computer " + c[i]. Resulting in "Computer 1", "Computer 2", "Computer 3" etc, not sure how to do that correctly. And an individual bet and guess to be assigned to each individual object. Right now i think its just displaying a random number rather than assigning that value to the particular object.
The bet and guess member variables shouldn't be static.
To display the id you can add a new int member variable, set it to i for each computer when you initialize them in the loop, and update the display() method to print it.
public class Computer {
int id;
int bet;
int guess;
int cash;
static Computer[] c;
public static void create(int numComps) {
c = new Computer[numComps];
for (int i = 0; i < numComps; i++) {
c[i] = new Computer();
c[i].id = i;
c[i].cash = Game.startCash;
c[i].bet = bet();
c[i].guess = guess();
c[i].display();
}
}
public static int bet() {
return Rng.rand(Game.startCash / 50) * 50;
}
public static int guess() {
return Dice.roll();
}
public void display() {
String name = "Computer ";
System.out.println("My name is " + name + id + " bet " + bet + " and guess " + guess);
}
}
I am very new to Java Programming. For example, even if I roll the same number i still lose the bet. If I roll like one and fine, I still win the bet amount. I am trying to fix that problem for hours. But can't figure it out. Please, someone, help me. Thanks in advance.
Here is my code.
public class Dice {
private int dice;
public Random number;
//default constructor
public Dice() {
number = new Random();
}
//To generate random number between 1 to 6. random starts from 0. there is no 0 on dice.
//By adding one, it will start at 1 and end at 6
}
//Method to check two dice
public boolean isequal(int dice1,int dice2) {
}
else
}
public class Playgame
{
//
}
public static void main(String[] args) {
//
}
}
{
return false;
}
}
userinput.close();
}
}
At least one problem is here (there may be others) :
if(obj1.isequal(obj1.play(), obj1.play()) == true)
{
System.out.println("You rolled a " + toString(obj1.play()) + " and a "
+ toString(obj1.play()) );
When you print the message, you are calling obj1.play() again and generating 2 new random numbers. If you need to use the value twice (once for comparison and once for printing) then you should store it in a variable.
int firstRoll = obj1.play();
int secondRoll = obj1.play();
if(obj1.isequal(firstRoll, secondRoll) == true)
{
System.out.println("You rolled a " + toString(firstRoll) + " and a "
+ toString(secondRoll) );
//...
Each call to obj1.play() return a different values.
Hence your test: obj1.isEqual(obj1.play(), obj1.play()) will mostly not return true.
no need for the dice class if it is to generate the random number and checks whether two number is equal or not. try the code below it will work
Random random = new Random();
int n1 = random.nextInt(6) + 1;
int n2 = random.nextInt(6) + 1;
System.out.println("You rolled a " + toString(n1)+ " and a " +toString(n2));
if (n1 == n2) {
double win = betmoney * 2;
System.out.println("You win $" + win);
startmoney += win;
} else {
startmoney -= betmoney;
System.out.println("You lose $" + betmoney);
System.out.println("You left only $" + startmoney);
}
problem with your code is your generating random numbers two times 1.during condition check and 2. in the sysout statement. your program is working fine only. but due to this your confusing yourself that it.
Each time you call ob1.play() method, it will give you different numbers.
in if clause:
if(obj1.isequal(obj1.play(), obj1.play()) == true)
will give you two random values that different from two random values in if block:
System.out.println("You rolled a " + toString(obj1.play()) + " and a " + toString(obj1.play()) );
So I just whipped up this quick little demo game in like 30 minutes and I was wondering 2 things:
How could I organize my code more?
Would you be willing to play a game like this?
I know that I could use classes but I'm a bit inexperienced them. I'm confused on how to get variables from specific classes. Would I need to import them into the main method class?
import java.util.Scanner;
public class mainGame
{
public static Scanner kboard = new Scanner(System.in);
public static boolean loop = true;
public static int treesInArea = 0;
public static int day = 0;
public static int wood = 0;
public static int woodCollected = 0;
public static int woodLevel = 0;
public static void main(String[] args)
{
System.out.println("__________________________________");
System.out.println(" Welcome to seul...Lets begin ");
System.out.println(" You woke up in the middle of ");
System.out.println(" a forest. Use the command walk ");
System.out.println(" in order to walk into a new area ");
System.out.println("__________________________________\n");
while(loop == true)
{
String choice = kboard.nextLine();
if(choice.equalsIgnoreCase("walk"))
{
treesInArea = (int)(Math.random() * 20);
System.out.println("__________________________________");
System.out.println("The number of trees in this area is");
System.out.println(treesInArea + " trees");
System.out.println("__________________________________\n");
day++;
System.out.println(" It is day " + day + " ");
System.out.println("__________________________________\n");
System.out.println(" Current usuable commands are : ");
System.out.println(" - Chop tree\n");
} else
if(choice.equalsIgnoreCase("choptree") || choice.equalsIgnoreCase("chop tree"))
{
if(treesInArea < 1)
{
System.out.println("There are no trees in this area.");
} else
{
woodCollected = (int)(Math.random() * 10);
treesInArea --;
wood += woodCollected;
woodLevel += (int)(Math.random() * 2);
System.out.println("__________________________________");
System.out.println(" You collected " + woodCollected + " wood");
System.out.println(" Your total wood = " + wood);
System.out.println(" Your total woodcutting level = " + woodLevel);
System.out.println("__________________________________\n");
}
}
}
}
}
You could improve your code in 4 main ways:
1 • Your code-indentation is not great, it should be a 4 space(or just press tab) indent after class name, loops, if statements etc. Example:
private methodName() {
for(int i = 0; i < 10; i++;) {
//do something
}
}
2 • It is easier to read your code when braces are right after methods/loops, and it takes less space, such as(5 lines of neat code):
if (condition = true) {
//do something
} else {
//do something else
}
Rather than(7 lines of messy code):
if (condition = true)
{
//do something
} else
{
//do something else
}
because when you have long if-else blocks, or long loops, it can become hard to read.
3 • You do not need to add spaces after a line, this does nothing. So this:
System.out.println(" It is day " + day + " ");
Can become this:
System.out.println(" It is day " + day);
4 • Lastly, the best way to organize code is by "dividing and conquering". This means make methods even if they're very short, to prevent repeat-code and save time. For example, you printed this line: System.out.println("__________________________________"); 7 times in your program. If you make a method like the one below, you can save time and space, by avoiding repeat-code, and simply call the method using printDivider(); wherever you used this line:
private static void printDivider() {
System.out.println("__________________________________");
}
Yes, I would play your game(in fact i did play your game), but you could improve it, by adding more possibilities, or different 'paths' to go down, ending in different results.
So I have the game already programmed. You take turns against the computer picking 1-3 straws and the point of the game is to leave the opponent with 1 straw. That entire section works, but now I have to program the Computer to get smarter through consecutive play.
I'm not entirely sure how this is done, however. The way it was explained to me was that you have 4 "cups", which I assume are arrays. Each cup contains the moves, (1, 2, 3). During the computer's turn it randomly picks a cup and randomly chooses one of the three moves. If that move doesn't work then it removes it from the cup.
After several games the computer should become unbeatable.I'm going to keep working on this but I'm having a lot of trouble. I've been at it for a couple hours now so I'll just post the raw code without my broken parts.
EDIT: I've added the Cup class at the bottom.
import java.util.*;
public class Nim
{
public static void main(String[] args)
{
Random r = new Random();
Scanner kb = new Scanner(System.in);
String reply;
int straws, cupNum, prevCupNum, prevComputerMove,
computerMove, humanMove, gameNumber = 0, humanWin = 0;
// create an array of four cups
Cup[] cup = new Cup[4];
for (int i = 0; i < cup.length; i++)
cup[i] = new Cup();
System.out.println("Let's play Nim");
System.out.println();
do
{
gameNumber++;
straws = r.nextInt(11) + 10;
// add 1 if necessary so computer nevers starts in losing config
if (straws%4 == 1)
straws++;
System.out.println("Straws to start = " + straws);
System.out.println();
// set to -1 to indicate just starting so no previous move
prevCupNum = -1; // no prev move so init to -1
prevComputerMove = -1;
do
{
cupNum = straws%4; // get cup number
// if cup is empty, then use 1 for move
if (cup[cupNum].count() == 0) // cup empty then move = 1
computerMove = 1;
else
computerMove = cup[cupNum].select(); // get move from cup
/*MISSING CODE
If cup is cup number 1, then remove the previous move
from the previous cup unless already empty*/
System.out.println
("Computer picks up " + computerMove + " straws");
straws = straws - computerMove;
if (straws < 0)
straws = 0;
System.out.println(straws + " left");
System.out.println();
// save this move so it can be removed later if necessary
prevCupNum = cupNum;
prevComputerMove = computerMove;
if (straws == 0)
{
System.out.println("Wow. You win!");
humanWin++;
/* MISSING CODE
Remove last move computer made from cup*/
}
else // get move from human
{
System.out.println();
do
{
System.out.print("Your move: enter ");
if (straws == 1)
System.out.println("1");
else
if (straws == 2)
System.out.println("1 or 2");
else
if (straws >= 3)
System.out.println("1, 2, or 3");
humanMove = kb.nextInt();
} while
(humanMove > 3 || humanMove < 1 || straws - humanMove < 0);
straws = straws - humanMove;
System.out.println(straws + " left");
if (straws == 0)
{
System.out.println();
System.out.println("Ha, ha. You lose!");
}
}
} while (straws > 0);
System.out.println
("Score: Human " + humanWin + " Computer " +
(gameNumber - humanWin));
if (kb.hasNextLine()) // get rid of stray newline
kb.nextLine();
System.out.println();
System.out.println("Want to play another game? Hit enter.");
System.out.println
("Or are you too CHICKEN? In that case, type in quit.");
reply = kb.nextLine();
reply = reply.trim();
if (reply.length() > 4) // require only 1st 4 to be quit
reply = reply.substring(0, 4);
}
while (!reply.toLowerCase().equals("quit"));
}
public class Cup
{
ArrayList<Integer> c = new ArrayList<Integer>();
public Cup()
{
c.add(1);
c.add(2);
c.add(3);
}
//-----------------------
public int count()
{
return c.size();
}
//-----------------------
public int select()
{
// random is a static method in Math that returns a double
// value greater than or equal to 0.0 and less than 1.0.
int index = (int)(c.size()*Math.random());
return c.get(index);
}
//-----------------------
public void remove(Integer move)
{
c.remove(move);
}
}
I'm afraid I don't understand the 'cup' strategy. There is a much simpler strategy to force the machine to learn through play, however. As it plays, record the number of straws it leaves after its turn. If it loses then ensure that it doesn't leave that many straws in turns in subsequent games.
Your code would look something like:
class AI_Player {
private final Set<Integer> losingMoves = new HashSet<>();
private final Set<Integer> currentGameMoves = new HashSet<>();
public void startGame() {
currentGameMoves.clear();
}
public int nextMove(int strawsRemaining) {
List<Integer> possibleMoves = Stream.of(1, 2, 3)
.filter(move -> move < strawsRemaining)
.filter(move -> !losingMoves.contains(strawsRemaining - move))
.collect(Collectors.toList());
int move = possibleMoves.get(Random.nextInt(possibleMoves.size()));
currentGameMoves.add(strawsRemaining - move);
return move;
}
public void registerLoss() {
losingMoves.addAll(currentGameMoves);
}
public void registerWin() {
losingMoves.removeAll(currentGameMoves);
}
}
This uses Java 8 streams but it's fairly trivial to convert if you haven't used them yet.
I'm currently working on a java project, and im supposed to make a program that generates a number between 1000 and 2000, then gives you the chance to enter the amount of players, takes in all the names and guesses in two different arrays, then create two methods, one to find the closest guess to the actual numbers and another to report or find the winner match it up with their guess and print it out. Im having trouble with my second method, im drawing a blank on the logic part right now this is my code and output:
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.");
}
}
}
}
This is the output it is producing when there is more than one winner.
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.
This is what it should look like
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 having trouble with the method portion i know this much but if you need to see the rest of the code let me know any help would be appreciated, thanks.
(I think i need a way to void the first print out if the counter passes one but im not sure how to go about this)
Try this. I have made a lot of changes in your code because I did not understand it. Following changes:
No raw types
Only one loop to find the winner
Only one array with the player and their numbers
A different loop for the output
Here is the code
import java.util.ArrayList;
import java.util.List;
public class GuesNumber {
public static void main(String[] args) {
// The number we want to guess
final int theNumber = 1500;
// A random set of players and their numbers
List<Player> players = new ArrayList<>();
for (int i = 0; i < 100; i++) {
players.add(new Player("Player " + i, (int) (Math.round((Math.random() * 1000)) + 1000)));
}
// Call of your function (with less arguments)
reportWinner(theNumber, players);
}
public static void reportWinner(int theNumber, List<Player> players) {
// Here we store the minimal diff between THE number
// and the number of the potential winner
Integer minDiff = null;
// Here we store the winners
ArrayList<Player> winners = new ArrayList<>();
// Find the winners
for (Player player : players) {
int diff = Math.abs(player.number - theNumber);
// We have a potential winner the player is the first
// we check or his number is less or equal then
// the minimal diff
if (minDiff == null || minDiff <= diff) {
// We have to clear all potential winners,
// if the number of the player is smaller then
// the minimal diff
if (minDiff != null && minDiff > diff) {
winners.clear();
}
// Add a potential winner
winners.add(player);
minDiff = diff;
};
}
// Let's do the output
System.out.println("There were " + theNumber + " jelly beans in the jar\n");
System.out.println("The winner " + (winners.size() == 1 ? "is" : "are") + ":\n");
for (Player player : winners) {
System.out.print(player.name + " with a gues of " + player.number + ", wich is " + minDiff + " away from the actual number of jelly beans. ");
}
System.out.println("");
}
public static class Player {
String name;
int number;
public Player(String name, int number) {
this.name = name;
this.number = number;
}
}
}
This is what you need. Note: it is recommended to use meaningful variable names rather than array or tempList to make the code more readable.
public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
ArrayList tempList = new ArrayList();
ArrayList tempList1 = new ArrayList();
int temp = 0;
for(int index = 0; index < array.length; index++)
{
temp = answer - array[index];
temp = Math.abs(temp);
if(temp == winner)
{
tempList.add(array1[index]);
tempList1.add(array[index]);
}
}
if(tempList.size() > 1) {
System.out.println("The winners are: ");
} else if(tempList.size() == 1) {
System.out.println("The winner is: ");
}
for(int index = 0; index < tempList.size(); index++)
{
String suffix = "";
if(winner == 0) {
suffix = "the exact number of jelly beans in the jar."
} else {
suffix = winner + " away from the actual number of jelly beans."
}
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is " + suffix);
}
}