Results 1 to 4 of 4
Thread: Need help with season program
- 09-27-2011, 05:39 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
Need help with season program
Following some pseudo code, I created this program.
It appears to me to be flawless, but it apparently is flawed because the seasons in the output are almost always wrong (except for month 3, for some reason).Java Code:import java.util.Scanner; public class Season { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Define Classes int month; int day; String season = "season"; // User input System.out.print("Enter a month and day: "); month = in.nextInt(); day = in.nextInt(); // Determine season if (month <= 3){ season = "winter"; } else if (month <= 6){ season = "spring"; } else if (month <= 9){ season = "summer"; } else if (month <= 12){ season = "fall"; } if (month % 3 == 0 && day >= 21) { if (season == "winter") season = "spring"; } else if (season == "spring"){ season = "summer"; } else if (season == "summer"){ season = "fall"; } else { season = "winter"; }[/b] // print if (month >= 13 || month < 1 || day > 31 || day <= 0) { System.out.println("Please enter a valid date"); } else { System.out.println(season); } } }
Are there any apparent mistakes you guys can see? I've been at this for quite a few hours and it just doesn't make sense to me...I'm sure it is something simple though.
UPDATE::
Wow that was fast, but after I decided to try one more thing, I got it to work!
I simply had the swirly brackets misplaced on last part.
changed toJava Code:if (month % 3 == 0 && day >= 21) { if (season == "winter") { season = "spring"; } else if (season == "spring"){ season = "summer"; } else if (season == "summer"){ season = "fall"; } else { season = "winter"; } }Last edited by Aimforthehead; 09-27-2011 at 05:45 PM.
-
Re: Need help with season program
Never compare Strings with == but instead use the equals or equalsIgnoreCase method. == checks to see if two String objects are one and the same, and you really don't care about that while equals checks if two Strings hold the same chars in the same order, and that's what's important to you.
So instead of:
do:Java Code:if (season == "winter") {
or:Java Code:if (season.equals("winter")) {
Java Code:if (season.equalsIgnoreCase("winter")) {
- 09-27-2011, 05:47 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
Re: Need help with season program
Okay I will change that as well.
-
Re: Need help with season program


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks