Exercise 5: Write a program that reads an integer. If the integer is between 1 and 4, then the
program should print one, two, three, or four, respectively. If the integer is not
between 1 and 4, then the program should print Number not between one and four.
Use a switch statement.
import java.util.*;
public class Arithmetic {
public static void main(String[] args) {
System.out.print("Enter a number from 1 to 4: ");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
switch(x){
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
case 4:
System.out.println("Four");
break;
default:
System.out.println("That is not a number from 1 to 4");
}
input.close();
}
}
No comments:
Post a Comment