• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on if statements and random numbers  RSS feed

 
Reiji Tokisaka
Greenhorn
Posts: 3
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

So I have a question. I am writing a simple program with if statements, in my example, if the weather is hot something happens. If the weather is cold, something else happens.

So this is what I have thus far:



It is obviously incomplete. I have no idea how to print "It seems the weather right now is " + cold or hot (depending on the randomization above));

My statement above prints "It seems the weather right now is either 1 or 0".

Does my question make sense?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... you need someway to translate numbers to a corresponding text value. There are any number of approaches I can think of. But this smacks of a school assignment, rather than a real world problem, so I'm not sure what approach aligns with something you're supposed to know at this point.

You learn about Maps yet? Arrays? The ternary operator? switch statements?

You can always fall back to a simple if statement.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, if you want the statement to print out regardless of the weather condition, your existing if statement doesn't really make sense, does it?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, you learn enums yet? (Probably the first question I should have asked.)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my advice...turn off your computer. Stop thinking about what JAVA to write, and start thinking about what you need to do. Think in English or any other human language you prefer. talk through the steps, as if you are speaking to a child. literally write out the steps long hand on paper.

When you have the steps written out, and ONLY when you have them written out, should you turn your computer back on and try to write the code in Java.

Also, your code is kind of messy. The indentation is all over the place. Get in the habit NOW of properly formatting your code. It makes life easier for you, for us, and for your teacher. It may seem trivial now, but it's WELL worth it in the end.
 
Reiji Tokisaka
Greenhorn
Posts: 3
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:So... you need someway to translate numbers to a corresponding text value. There are any number of approaches I can think of. But this smacks of a school assignment, rather than a real world problem, so I'm not sure what approach aligns with something you're supposed to know at this point.

You learn about Maps yet? Arrays? The ternary operator? switch statements?

You can always fall back to a simple if statement.


Yes, I need to do translate the numbers generated to a corresponding text value to print on the screen. Also, this is a school assignment. Wasn't sure if I should've mentioned that because I did not see its relevancy then, but I suppose you should know at least what I know thus far! No maps, no arrays, but switch is on today's upcoming lecture so that would be okay.
 
Reiji Tokisaka
Greenhorn
Posts: 3
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Here's my advice...turn off your computer. Stop thinking about what JAVA to write, and start thinking about what you need to do. Think in English or any other human language you prefer. talk through the steps, as if you are speaking to a child. literally write out the steps long hand on paper.

When you have the steps written out, and ONLY when you have them written out, should you turn your computer back on and try to write the code in Java.

Also, your code is kind of messy. The indentation is all over the place. Get in the habit NOW of properly formatting your code. It makes life easier for you, for us, and for your teacher. It may seem trivial now, but it's WELL worth it in the end.


Yeah, I actually made a flowchart before I started, and I am in the process of translating said flowchart into code. I am just stuck, unsure how to turn that random generated number (a 1 or 0) into cold or hot. Sorry if my question didn't really make sense!

By the way, you mean the indentation of lines 11 & 12? They are properly aligned on eclipse, I just pasted here and didn't realize they looked a little funky on the posting, sorry.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reiji Tokisaka wrote:Yeah, I actually made a flowchart before I started, and I am in the process of translating said flowchart into code. I am just stuck, unsure how to turn that random generated number (a 1 or 0) into cold or hot. Sorry if my question didn't really make sense!

By the way, you mean the indentation of lines 11 & 12? They are properly aligned on eclipse, I just pasted here and didn't realize they looked a little funky on the posting, sorry.

I prefer pseudo code over flow charts. Example:
or, probably better

BTW, indentation gets screwed up when you have a mix of spaces and tabs. In eclipse, to correct indentation highlight a block of code and press Ctrl-i. I believe Code Ranch assumes that tabs are equivalent to 4 spaces. Eclipse has a preference for that: Window > Preferences > General > Editors > Text Editors > Display tab width.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reiji Tokisaka wrote:I am just stuck, unsure how to turn that random generated number (a 1 or 0) into cold or hot.

Weather is an int. The value in this int already represents hot or cold. You have two (should be) constants, "hot" and "cold" with corresponding integer values. Now, you could compare weather to 0 and that would tell you if it's hot, but seeing as you have a constant called "hot" with the correct value, you could compare weather to your hot constant.

Your "constants" will work in your case ONLY if you don't modify their values. Here is how constants are usually written:

"private" no one outside this class can use it.
"static" only one for the class instead of one for every class instance.
"final" can't be modified.
"int" this is what we need for HOT/COLD but the type could be something else for other constants.
"HOT" this is a variable name in all caps. Usually variables use camel-case, but not for constants.
"= 0" the constant must be initialized here.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reiji Tokisaka wrote:Also, this is a school assignment. Wasn't sure if I should've mentioned that because I did not see its relevancy then

Of course it's relevant. For school assignments you are limited to only using what you have learned or are supposed to be learning. In the real world, approaches and techniques that are beyond the scope of the assignment are likely to be suggested.

So, have you learned enums yet?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!