Question

In: Computer Science

I need a java code Write a simple program to prompt the user for a number...

I need a java code

Write a simple program to prompt the user for a number between 1 and 12 (inclusive)
and print out the corresponding month. For example:

  The 12th month is December.

Use a java "switch" statement to convert from the number (1-12) to the month.

Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12
inclusive and if not, prompt the user again until getting the correct value.

Solutions

Expert Solution

Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.

Working of the code

  • do while loop prompt user to enter month's number untill user enter a valid number
  • When the entered number is not in the range between 1 and 12, the loop body executes again and prompt user to enter month's number again.
  • switch statement compare monthNumber with case values.
  • Whenever a match is found,the statement associated with that block will be executed and then exit from the switch statement
  • If no match is found,the statement in the default will be executed

Source code

//Importing Scanner class to read user input
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        //Creating scanner class object to read user input
        Scanner sc = new Scanner(System.in);
        //declaring an integer variable monthNumber
        int monthNumber;
        //loop exit when user enter a valid number
        //if the user input is not in the valid range,loop executes its body again
        do {
            System.out.print("Enter month's number: ");
            monthNumber = sc.nextInt();
        }while(!(monthNumber>=1 && monthNumber<=12));//condition to repeat the loop

        //switch statement compare monthNumber with each case values.Whenever a match
        //is found, the statement associate with the match will be executed
        switch (monthNumber) {
            case 1:
                System.out.println("January");
                break;
            case 2:
                System.out.println("February");
                break;
            case 3:
                System.out.println("March");
                break;
            case 4:
                System.out.println("April");
                break;
            case 5:
                System.out.println("May");
                break;
            case 6:
                System.out.println("June");
                break;
            case 7:
                System.out.println("July");
                break;
            case 8:
                System.out.println("August");
                break;
            case 9:
                System.out.println("September");

                break;
            case 10:
                System.out.println("October");
                break;
            case 11:
                System.out.println("November");
                break;
            case 12:
                System.out.println("December");
                break;
                //if no match found
            default:
                System.out.println("Invalid entry.");
                break;

        }
    }
}

Screen shot of the code

Screen shot of the output


Related Solutions

I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
In Python, your program will read in a number (no need to prompt the user), and...
In Python, your program will read in a number (no need to prompt the user), and then reads in that number of lines from the terminal. Then the program should print an array of strings formatted in a nice regular box. So if the user inputs this: 5 Grim visaged war has smooth’d his wrinkled front And now, instead of mounting barded steeds To fright the souls of fearful adversaries He capers nimbly in a lady’s chamber To the lascivious...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
Write a java program that prompt the user to insert last term’s courses and their individual...
Write a java program that prompt the user to insert last term’s courses and their individual mark and store them in an appropriate multidimensional array.
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT