• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic if/else help  RSS feed

 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am required to run an if/else loops that reflects the following rating hygiene system. Any ideas on how to do this?

5 = excellent
4 = very good
3 = good
2 = good
1 = good
0 = not applicable

I know that I am starting with : public static String interpret (double rating){

any help would be appreciated...

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you got so far?

Even some pseudo code? Steps in plain english explaining how your program is going to work.
For this case the english should be actually reasonably close to the java code required.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why double when the ratings are integers?
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is my code so far. Im pretty new to java, but for some reason this doesn't run. I know the syntax is terrible... Any chance someone could fix this for me?
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why double when the ratings are integers?


Sorry, forgot to mention, the ratings are read as 5.0, 4.0, 3.0 etc.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Z Howard wrote: Any chance someone could fix this for me?

Sorry but we don't do that here. However, we will explain what is wrong and help you to be able to fix yourself though.

But before I do that please can you explain to me what these lines of code are supposed to be doing
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also be sure you use correct and consistent indentation. It's not just for appearance -- it's an important tool to understanding the structure of the statements.
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Z Howard wrote: Any chance someone could fix this for me?

Sorry but we don't do that here. However, we will explain what is wrong and help you to be able to fix yourself though.

But before I do that please can you explain to me what these lines of code are supposed to be doing


I thought of actually declaring the variable and its values? So to be between 0 and 5.0. Or is that not required??
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you returning anything from that method? Shouldn't it have return interpret; at the end?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought of actually declaring the variable and its values? So to be between 0 and 5.0.

But your next line tests if the value is equal to or greater than 5.0 and the last line tests if the value is equal to or less than 0.

BTW when writing if statements and loops always use curly braces, not doing so can make the code so confusing to read especially for beginners.

Using doubles to represent integer values is plain wrong but if that is what you have been told to do then you are stuck with it and you possibly have to allow for values such as 5.00000001 and 4.999999999
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Are you returning anything from that method? Shouldn't it have return interpret; at the end?


I actually added that but it was giving a lot of compile errors?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of compile errors do you get?

The code sample you posted does not compile - not enough closing braces at the end.
And then as Campbell mentioned, you need to return a value from your method.

Apart from that, it looks as though it should work.

With regards to doubles vs integer, can a user input a rating of 3.7?
If you are going to do that, then I would suggest making your tests should be for greater than/less than rather than equals.
Would a rating of 3.7 be interpreted as "Good" (i.e. > 3) or "Very Good"? (It is closer to 4 if you round up...)


 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Z Howard wrote:I know that I am starting with : public static String interpret (double rating){

Its been mentioned already by Stefan Evans, there is a big probability you made a wrong assumption at the beginning.
Have you solved that exercise in some kind of simpler form? For instance on a piece of paper with a pseudocode content on it or simply plain english, or other native language? If not, could you simply describe in few sentences what has to be done in this exercise?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your current if blocks look like this:

Is it what you expect?

That could be simplified probably. Snippet below has no effect (redundant).

And after that you can go to further simplifications.
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys the following compiles... but I get an error that there isnt a main method found. Do I need to add "public static void main(String[] args)" somewhere??

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Z Howard wrote:. . .
double rating
. . .
double ratings
. . .
It might compile but if you have two variables with such similar names you are guaranteed to confuse yourself. Go through your method and work out what it will return. Then get rid of the non‑parameter variable.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what will happen if I give you a rating of 4.8?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Likely you want to replace those == to >=. And I'm not sure about <= next to the 3.5.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!