• Post Reply Bookmark Topic Watch Topic
  • New Topic

java code problem  RSS feed

 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...

please help
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not given sufficient information to give your question's answer such as you have to show the output and class Player.
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.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try out with the following:

 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using three similar variables rather than an array? Why have you got the three booleans separate from the players, rather than as fields of the player class?
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
Gaurangkumar Khalasi...
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell Ritchie,

I will try with your suggestion.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abrar alvi wrote:Thank You
Gaurangkumar Khalasi...


You are welcome
 
Steven Schwab
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 " 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.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Steven Schwab... thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!