• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Can someone help me print the results in my GuessingGame code?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 8544
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adnan Alvi, welcome to the Ranch.

Before you start contributing in this nice community, please have a look how to UseCodeTags (<- link to click on). Posting your code by using code tags, will make your code look nice. I have added them for you this time, see how better it looks now? Next time you'll know how to make it yourself

Could you please be more specific what kind of issues you're facing? Which parts of the code does not do the things in the way you're expecting them to do?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In Java, local variables are ... well ... local to the block where it is declared. It can't be used in another method. Also, there is no guaranteed that a local variable is even still in scope when another method is running.

If you want a method to use values held in another method's local variables, you have a few options. You can pass those values via parameters. Or you can store those values in variables that have a larger scope, such as static/instance variables.

Henry
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

Another formatting thing: // comments are intended for something short. If you need longer comments always use /* comments */. The comments make the lines too long to read easily; I shall go back and change the one long comment so you can see how it should be done.
 
Campbell Ritchie
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long comment on line 44 changed. Note I have moved it to before what it describes.
 
Liutauras Vilda
Sheriff
Posts: 8544
615
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also,

change it to:

Maybe it looks not that important, but it is rather important. Never write while "something == false" or "something == true", write while(something) or while(!something).
If you ever manage to write "while (value = true)" you'll likely get undesired program flow.
 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 56: play(s) is a recursive call. You don't need to make this call at all, just continue on with the logic and the while() loop will do the right thing.
Edit: You'll need to generate a new random number.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic