Home > Back-end >  How do i compare my guess with other computers to get the highest bid
How do i compare my guess with other computers to get the highest bid

Time:12-21

How can I make this print out the highest bid in java?

I am trying to make a Price is Right game but i cant figure out how to print the highest bidder

    int computerOne = random.nextInt(2000)   1001;
    int computerTwo = random.nextInt(2000)   1;
    int computerThree = 0;

    if (userGuess > computerOne) {
        computerThree = random.nextInt(userGuess)   (2000);
    } else if (userGuess < computerTwo) {
        computerThree = random.nextInt(computerOne - 500)   (userGuess 1);
    }
    
    System.out.println("Alright everyone's bids are in: \n\tContestant 1: $"   userGuess   " \n\tContestant 2: $"   computerOne   " \n\tContestant 3: $"   computerTwo   "\n\tContestant 4: $"   computerThree);

    int correctPrice = random.nextInt(400) 201;
    System.out.println("The retail price of the item was $"   correctPrice);

    int userDiff = (correctPrice - userGuess);

    int computerDiff1 = (correctPrice - computerOne);

    int computerDiff2 = (correctPrice - computerTwo);

    int computerDiff3 = (correctPrice - computerThree);


    if (userGuess < computerOne && userGuess < computerTwo && userGuess < computerThree)
        System.out.println("Player one wins");
    else if (userGuess < computerTwo && userGuess < computerOne && userGuess < computerThree)
        System.out.println("Player two wins");
    else if (userGuess < computerThree && userGuess < computerOne && userGuess < computerTwo)
    System.out.println("Player three wins");
}

}

CodePudding user response:

Use a list to track all the bids so you can iterate the list instead of having to write so many different if/else conditions. That is very error prone.

This is how I suggest doing it if you haven't learned to write your own classes yet. Make a list of the bids. Iterate the list to find the best bid that fits the criteria of being no more than the correctPrice. At the same time, keep track of which index of the list corresponds with the best bid found. Then you can use that to name the winner. You can use a negative value for the default index so you know if no one made a valid bid.

The following example code would replace the int userDiff line and everything below it.

var bids = List.of(userGuess, computerOne, computerTwo, computerThree);

int bestBid = 0;
int bestBidIndex = -1;
for (int i = 0; i < bids.size(); i  ) {
    int bid = bids.get(i);
    if (bid > bestBid && bid <= correctPrice) {
        bestBid = bid;
        bestBidIndex = i;
    }
}
if (bestBidIndex >= 0) {
    var winner = "Contestent "   (bestBidIndex   1);
    System.out.println("The winner is "   winner   " with a guess of $"   bestBid   ".");
} else {
    System.out.println("There is no winner. All players busted.");
}

CodePudding user response:

You could use a variable called highest bid, and compare it to each of the existing bids.

IE-

int highestBid = computerOne;
if (computerTwo > highestBid)
    highestBid = computerTwo;

Putting this into some sort of list of bids would make this cleaner and easier to sort through.

  •  Tags:  
  • java
  • Related