posted 5 years ago

- 1

try something like

if (p1right || p2right || p3right) {

System.out.println("WE HAVE A WINNER");

System.out.print("THE WINNER IS ");

StringBuffer result = new StringBuffer();

if(p1right)

result.append("P1 ");

if(p2right)

result.append("P2 ");

if(p3right)

result.append("P3 ");

System.out.println(result);

}

if (p1right || p2right || p3right) {

System.out.println("WE HAVE A WINNER");

System.out.print("THE WINNER IS ");

StringBuffer result = new StringBuffer();

if(p1right)

result.append("P1 ");

if(p2right)

result.append("P2 ");

if(p3right)

result.append("P3 ");

System.out.println(result);

}

Gaurangkumar Khalasi

Ranch Hand

Posts: 187

posted 5 years ago

You have not given sufficient information to give your question's answer such as you have to show the output and class Player.

But as per the given information, i can say that "for three players", you have not set condition...

e.g.

@harshvardhan ojha

He has shown a nice way rather than defined in your code to get the result...You can use it. Just defined the StringBuffer result = new StringBuffer(); outside the while loop.

abrar alvi wrote:the problem here is if two or three players are right in guessing i am not able to display the correct System.out.println statement...

But as per the given information, i can say that "for three players", you have not set condition...

e.g.

@harshvardhan ojha

He has shown a nice way rather than defined in your code to get the result...You can use it. Just defined the StringBuffer result = new StringBuffer(); outside the while loop.

posted 5 years ago

This is the output which i get even after adding the && condition for all three players :

STARTING GAME

I AM THINKING OF A NUMBER FROM 0 TO 9

NUMBER TO GUESS IS: 4

P1 IS GUESSING NUMBER AS: 5

P2 IS GUESSING NUMBER AS: 0

P3 IS GUESSING NUMBER AS: 2

try again

NUMBER TO GUESS IS: 4

P1 IS GUESSING NUMBER AS: 6

P2 IS GUESSING NUMBER AS: 7

P3 IS GUESSING NUMBER AS: 5

try again

NUMBER TO GUESS IS: 4

P1 IS GUESSING NUMBER AS: 4

P2 IS GUESSING NUMBER AS: 4

P3 IS GUESSING NUMBER AS: 6

WE HAVE A WINNER

THE WINNER IS P1 AND P2 AND P3 // this is not correct

(before i even got only one player as winner for 2 players guessing the correct number)

Here is the player class:

PLEASE HELP...

You have not given sufficient information to give your question's answer such as you have to show the output and class Player.

This is the output which i get even after adding the && condition for all three players :

STARTING GAME

I AM THINKING OF A NUMBER FROM 0 TO 9

NUMBER TO GUESS IS: 4

P1 IS GUESSING NUMBER AS: 5

P2 IS GUESSING NUMBER AS: 0

P3 IS GUESSING NUMBER AS: 2

try again

NUMBER TO GUESS IS: 4

P1 IS GUESSING NUMBER AS: 6

P2 IS GUESSING NUMBER AS: 7

P3 IS GUESSING NUMBER AS: 5

try again

NUMBER TO GUESS IS: 4

P1 IS GUESSING NUMBER AS: 4

P2 IS GUESSING NUMBER AS: 4

P3 IS GUESSING NUMBER AS: 6

WE HAVE A WINNER

THE WINNER IS P1 AND P2 AND P3 // this is not correct

(before i even got only one player as winner for 2 players guessing the correct number)

Here is the player class:

PLEASE HELP...

Campbell Ritchie

Marshal

Posts: 56530

172

Gaurangkumar Khalasi

Ranch Hand

Posts: 187

Steven Schwab

Greenhorn

Posts: 9

posted 5 years ago

- 1

Although data encapsulation is a great concept, I think the poster is suffering primarily from a lack of debugging skills.

@abrar alvi, try to think logically about what the code that you posted is doing:

1. You know that it is printing out "P1 AND P2 AND P3 " for the values 4, 4, and 6.

2. You know that each statement is wrapped in its own if condition.

Therefore, the if condition around "P1 AND P2 AND P3 "

Note that if the if condition looks correct, your next step would be to trace back to where each of the individual variables in the if condition are being set. This would have answered your first question in this topic, by finding that p3right was not being set.

@abrar alvi, try to think logically about what the code that you posted is doing:

1. You know that it is printing out "P1 AND P2 AND P3 " for the values 4, 4, and 6.

2. You know that each statement is wrapped in its own if condition.

Therefore, the if condition around "P1 AND P2 AND P3 "

**MUST**be getting evaluated to true with the values 4, 4, and 6. Look at that condition and you will find your error.Note that if the if condition looks correct, your next step would be to trace back to where each of the individual variables in the if condition are being set. This would have answered your first question in this topic, by finding that p3right was not being set.