Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with this Java programming assignment: Write a program that prompts the user to enter the name of any 30-day month. The program
I need help with this Java programming assignment:
Write a program that prompts the user to enter the name of any 30-day month. The program should respond positively or negatively as appropriate. Use a switch. "Stack" cases as shown on page 101.
3.13 switch Statements 101 case value2: statement (s)2: break: case valueN: statement (s)N; break default: statement (s)-for-default; The switch statement observes the following rules: The switch-expression must yield a value of char, byte, short, int, or String type and must always be enclosed in parentheses. (The char and String types will be introduced in Chapter 4.) The value1...., and valueN must have the same data type as the value of the switch-expression. Note that value1,..., and valueN are constant expressions, meaning they cannot contain variables, such as 1+ x. When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. The keyword break is optional. The break statement immediately ends the switch statement Caution Do not forget to use a break statement when one is needed. Once a case is matched. the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This is referred to as fall-through behavior. For example, the following code displays Weekday for days 1-5 and Weekend for day 0 and day 6. fall-through behavior switch (day) ( case 1 case 2: case 3: case 4: case 5: System.out.println("Weekday") break; case 0 case 6: System.out.println("Weekend"); Tip To avoid programming errors and improve code maintainability, it is a good idea to put a comment in a case clause if break is purposely omitted. Now let us write a program to find out the Chinese Zodiac sign for a given year. The Chi- nese Zodiac is based on a 12-year cycle, with each year represented by an animal-monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep-in this cycle, as shown in Figure 3.6. Note year % 12 determines the Zodiac sign. 1900 is the year of the rat because 1900 % 12 is 4, Listing 3.9 gives a program that prompts the user to enter a year and displays the animal for the year
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started