OneStepForward
OneStepForward

Reputation: 43

Playing Card Switch Programing

Here is the program requirements I'm trying to run. I actually prefer If and Else statements, but once again, the requirement is to only use switch statements.

Create a new Java class inside your project folder. The name of the class should be: CardConverter

Write a program that reads a 2-3 character string (which will be a shorthand notation describing a playing card) from the user and outputs the full description of the card.

To begin, you will get the string from the user. Input the string using a single nextLine command. The inputted string will consist of a letter or number: (A, J, Q, K, 2, 3, 4, 5, 6, 7, 8, 9, or 10) followed by another letter: (D, H, S, C)

After getting the input, your program should then print to the screen the full description of the card in the following format: VALUE of SUIT

Number cards should NOT be spelled out - just display the number; face cards, however, do need to be spelled out.

For example here are some possible input values, along with the expected output from the program:

If the user enters QS, your program should print: Queen of Spades If the user enters AH, your program should print: Ace of Hearts If the user enters 7C, your program should print: 7 of Clubs If the user enters 10D, your program should print: 10 of Diamonds If the user enters KC, your program should print: King of Clubs If the user enters JS, your program should print: Jack of Spades

Technical Notes & Requirements:

You are NOT ALLOWED TO USE "IF" STATEMENTS in this program! You must create this program by using SWITCH statements instead. Create a variable called result Use a SWITCH statement to assign the result variable an initial value - the value of the card Use a second SWITCH statement to concatinate to the result variable the card's suit You may assume that the input will be correct, and therefore you don't have to worry about adding any type of error handling possibilities.

This is my code so far. I run the program just testing case 1, but no output comes up, am I missing something?

//The program will describe a playing card after the user inputs a 2-3 character string.

import java.util.*;
public class CardConverter {

    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);

        //Variable decoration
        String userinput;

        //int ;



        System.out.print("Please input 2-3 characters by selecting from the following: A, C, D, E, H, J, K, Q, S, 2, 3, 4, 5, 6, 7, 8, 9 or 10: ");
        userinput = kbd.next().toUpperCase();

        switch (userinput){

            case "1": userinput.contains("AH");
            System.out.println("Ace of HEARTS");
        }
    }
}

Here is the updated version of the code so far:

//The program will describe a playing card after the user inputs a 2-3 character string.

import java.util.*; public class CardConverter {

public static void main(String[] args) {
    Scanner kbd = new Scanner(System.in);

    //Variable decoration
    String result;

    //int ;



    System.out.print("Please input 2-3 charactes by selecting from the following: A, C, D, E, H, J, K, Q, S, 2, 3, 4, 5, 6, 7, 8, 9 or 10: ");
    result = kbd.next().toUpperCase();

    switch (result){

        case "AH": 
            System.out.println("Ace of Hearts");
        break;

        case "AD":
            System.out.println("Ace of Diamonds");
            break;

        case "AC":
            System.out.println("Ace of Clubs");
            break;

        case "AS":
            System.out.println("Ace of Spades");
            break;

    }//Goes with switch









    kbd.close();
}

}

Upvotes: 1

Views: 1227

Answers (2)

KobiF
KobiF

Reputation: 60

OK... This is very simple all you have to do is change

    case "1":

to

    case "AH":

Upvotes: -1

nhouser9
nhouser9

Reputation: 6780

You are, indeed, missing something.

case "1": declares that if the switch variable (userinput) matches the case ("1") then the following code will be executed:

userinput.contains("AH");
System.out.println("Ace of HEARTS");

It seems like you think that userinput.contains("AH") is a condition being checked. That's not how switch statements work. Try:

case "AH":
  System.out.println("Ace of Hearts");

You should do research on how switch statements work before trying to write one yourself. A better place to start would have been google. A simple search for "java switch tutorial" found me this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Upvotes: 3

Related Questions