Write a Java program that will read numbers between 1 and 14 and will output the
ID: 3847494 • Letter: W
Question
Write a Java program that will read numbers between 1 and 14 and will
output the appropriate symbol for a playing card of that rank (use
switch).For cards between 2 and 10 just print the rank, for 1 and 11 print "Ace", for
12 print "Jack", for 13 print "Queen", and for 14 print "King". Use a for loop to read and process
n cards (input from user). Your program should also handle invalid input.
Sample output:
Playing cards program.
Give me
some numbers and
I will tell you the appropriate playing card.
=============================================
How many numbers?
-
20
ERROR!
Should be positive.
Reenter:
5
Enter card number:
23
ERROR!
Out of range (1
-
14).
Reenter:
11
You
have an Ac
e.
Enter card number:
14
You
have a King.
Enter card number:
2
You
have a two.
Enter card number:
13
You
have a Queen.
Enter card number:
1
You
have an Ace.
How would I do this in java?
Explanation / Answer
//import packages
import java.util.Scanner;
public class Lab7_1 {
public static void main (String[] args) {
// declarations
Scanner input = new Scanner(System.in);
int cardNumber;
char cardType;
System.out.println("Playing cards program.");
System.out.println("Give me some numbers and ");
System.out.println("I will tell you the appropriate playing card. ");
System.out.print("============================================= ");
System.out.println();
System.out.print("Enter Card number (2 - 14): ");
cardNumber = input.nextInt();
while(cardNumber < 2) {
System.out.print("ERROR! Should be positive Reenter: ");
cardNumber = input.nextInt();
}
while(cardNumber > 14) {
System.out.print("ERROR! Out of range. Reenter: ");
cardNumber = input.nextInt();
}
// output
switch(cardNumber){
case 2:
System.out.print("You have a Duece.");
break;
case 3:
System.out.print("You have a Three.");
break;
case 4:
System.out.print("You have a Four.");
break;
case 5:
System.out.print("You have a Five.");
break;
case 6:
System.out.print("You have a Six.");
break;
case 7:
System.out.print("You have a Seven.");
break;
case 8:
System.out.print("You have a Eight.");
break;
case 9:
System.out.print("You have a Nine.");
break;
case 10:
System.out.print("You have a Ten.");
break;
case 11:
System.out.print("You have an Ace.");
break;
case 12:
System.out.print("You have a Jack.");
break;
case 13:
System.out.print("You have a Queen.");
break;
case 14:
System.out.print("You have a King.");
}
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.