• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Guessing Game

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I am new to this and I am having problem figure out how to fix my code. I am a beginner and just want to learn. I know I have a lot to learn. So if someone could tell me what I am doing wrong. I would appreciate it. I am creating a guessing game for a user to select a number from 1-10 but when entering an answer, it does not tell me if I won or lost correctly. When I set it to false I lose and when I set it to true I win. But this is not what I want. But, when I answer correctly it says correct guess but the game does not end, it just continues. Please help I am stuck.

 
Saloon Keeper
Posts: 9350
78
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 27 you create and initialize 'a' but you never pass it to playGame(), so playGame() creates its own random number that is probably not the same as 'a' yet on lline 32 you print "...number was " + a, which is not true.

So far haven't looked beyond this.
 
Natasha Gomez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i put a in the playGame it gives me an error when i compile it. It says int cannot be converted into boolean
 
Carey Brown
Saloon Keeper
Posts: 9350
78
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
Always cut and paste the text of any error message into a post.

Which line number?
 
Carey Brown
Saloon Keeper
Posts: 9350
78
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
You get "choice" as an input parameter but you don't do anything with it except return it at the end.
 
Natasha Gomez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I get the error when I enter a into if (playGame(a)) {
 
Natasha Gomez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I also not understanding what I had to do with choice?
Sorry I am new to things and still learning the basics

 
Marshal
Posts: 75878
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can see some other things which haven't been mentioned yet; I shall only tell you about one mistake.
Never write == false or == true. Not only are they both poor style, but they are also error‑prone; every now and again we see somebody write = instead of == and they now have two errors for the price of one.
Never while (b == false) ...
Always while (!b) ...
Never while (b == true) ...
Always while (b) ...
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Natasha Gomez wrote:

I get the error when I enter a into if (playGame(a)) {


First of all, please post the entire error message you get.  But also notice that you speak of if (playGame(a)) but the code is if (playGame(x)).
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic