• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Changing String Value (if and else statements

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the assigment. I believe I am only allowed to use If and else statements




> The following algorith yields the season (Spring, Summer, Fall, or
> Winter) for a given month of the day.
>
> IF month is 1,2, or 3, season = "Winter
> Else if month is 4, 5, or 6, season = "Spring"
> Else if month is 7,8, 9, season = "Summer"
> Else if month is 10,11, or 12, season = "Fall"
> If month is divisible by 3 and day >= 21
> If season is "Winter", season = "Summer"
> Else if season is "Spring", season = "Spring"
> Else if season is "Summer", season = "Fall"
> Else season = "Winter"
>
> Write a program that prompts the user for a month and day and then
> prints the season, as determined by this algorithm


Heres what I got so far. I can't get the last part to get the seasons to change if the month is divisible by 3 and day is above 20.


 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the code tags that make your code more readable.
for the part that you didn't get is simple, you just need to check for all 3 conditions Month, day and season.

you can use nested if else in that condition just check for the month and day. then if season=x make it y, if season=p make it q like that..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

It seems you're almost finished with this. You just have to remove the System.out.println statements on lines 18, 22, 28 and 33 and print the value of the variable season at the end of the program instead (after line 53). Right now you are printing the value before the last part (lines 37-52) has executed, so obviously you don't see the result of the logic in that last part.

It also looks like you forgot something at line 39, there's an empty if-block there.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic