• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

java code problem

 
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try out with the following:

 
Marshal
Posts: 80666
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You
Gaurangkumar Khalasi...
 
abrar alvi
Ranch Hand
Posts: 66
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abrar alvi wrote:Thank You
Gaurangkumar Khalasi...



You are welcome
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Steven Schwab... thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic