My program has errors with certain inputs which are not shown to me directly and
ID: 3923473 • Letter: M
Question
My program has errors with certain inputs which are not shown to me directly and I need help fixing my code's flaws. Please helpObjectives 1. Use Scanner class to get user input and System.out.println() to display output.? 2. From pseudocode, convert a ladder-style nested if-else statement into a switch statement.? The following algorithm yields the season (Spring, Summer, Fall, or Winder) for a given month and day. Write a program that prompts the user for a month and day and then prints the season, as determined by this algorithm.
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, or 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 = "Spring" Else If season is "Spring", season = "Summer" Else If season is "Summer", season = "Fall" Else season = "Winter" My program has errors with certain inputs which are not shown to me directly and I need help fixing my code's flaws. Please help
Objectives 1. Use Scanner class to get user input and System.out.println() to display output.? 2. From pseudocode, convert a ladder-style nested if-else statement into a switch statement.? The following algorithm yields the season (Spring, Summer, Fall, or Winder) for a given month and day. Write a program that prompts the user for a month and day and then prints the season, as determined by this algorithm.
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, or 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 = "Spring" Else If season is "Spring", season = "Summer" Else If season is "Summer", season = "Fall" Else season = "Winter" -------------------------------------------------------
import java.util.Scanner; public class Lab3c { // Program 3, Eduardo Acosta, class Lab3c{ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int month = 0; int day = 0; String season = ""; System.out.println("Enter a month and day: "); month = scnr.nextInt(); day = scnr.nextInt(); switch (month) { case 1: case 2: case 3: System.out.println("Winter"); break; case 4: case 5: case 6: System.out.println("Spring"); break; case 7: case 8: case 9: System.out.println("Summer"); break; case 10: case 11: case 12: System.out.println("Fall"); break; } if (month % 3 == 0 && day >=21) { if (season.equals("Winter")){ season="Spring"; } else if (season.equals("Spring")) { season = "Summer"; } else if (season.equals("Summer")) { season = "Fall"; } else if (season.equals("Fall")) { season = "Winter"; } System.out.println(season); } } } If month is divisible by 3 and day>21 If season is "Winter", season "Spring Else If season is "Spring", season =·summer. Else If season is Summer, season "Fall" Else season"Winter" Requirements: The name of the Java class is Lab3c. . Your "identification string" should be the first comment of the main method.// Program 3, [Name], [mascId] The prompt should be on its own line with the following text f.e. use System.out.println)) Enter a month and day: .Read in the value and use a switch structure to determine which value the user entered. . A statement such as if ( input-1 I I input "2 I I input--3 ) can be accomplished in a switch statement employing the fall through" mechanism switch (input) case 1: case 2: case 3: SomeStatementHere; break; Strings, however, cannot be used with switch statements. For this you wilneed an if-else nested statement. Use if (season.equals("Winter")) . Test your code with several different months and days until you have tested every possible outcome. . Submit your code. The test data will not be made visible to you, so use the Develop mode and test wel
Explanation / Answer
Hi, I have fixed the error.
Actually when you enter month value out of range (not in 1-12) , it will not print any thing.
So, i added code to validate month and day input.
import java.util.Scanner;
public class Lab3c {
// Program 3, Eduardo Acosta, class Lab3c{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int month = 0;
int day = 0;
String season = "";
System.out.println("Enter a month and day: ");
month = scnr.nextInt();
day = scnr.nextInt();
// validating month
while(month<1 || month>12){
System.out.print("Enter month value in range 1-12: ");
month = scnr.nextInt();
}
// validating day
while(day<1 || day>31){
System.out.print("Enter day value in range 1-31: ");
day = scnr.nextInt();
}
switch (month) {
case 1:
case 2:
case 3:
System.out.println("Winter");
break;
case 4:
case 5:
case 6:
System.out.println("Spring");
break;
case 7:
case 8:
case 9:
System.out.println("Summer");
break;
case 10:
case 11:
case 12:
System.out.println("Fall");
break;
}
if (month % 3 == 0 && day >=21) {
if (season.equals("Winter")){
season="Spring";
}
else if (season.equals("Spring")) {
season = "Summer";
}
else if (season.equals("Summer")) {
season = "Fall";
}
else if (season.equals("Fall")) {
season = "Winter";
}
System.out.println(season);
}
}
}
/*
Sample output:
Enter a month and day:
13
21
Enter month value in range 1-12: 5
Spring
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.