Thinking in Java Fourth Edition Bruce Eckel Chapter 4 Exercise 22

Exercise 22: Write a switch statement for the enum in the previous example. For each case, output a description of that particular currency.

enum Bills
{
        ONE, FIVE, TEN, TWENTY, FIFTY, HUNDRED
}

public class Wallet
{
    Bills b;
    public static void main(String[] args)
    {
        for(Bills b : Bills.values())
        {
            System.out.print("Worth: ");
            switch(b)
            {
                case ONE: System.out.println("$1"); break;
                case FIVE: System.out.println("$5"); break;
                case TEN: System.out.println("$10"); break;
                case TWENTY: System.out.println("$20"); break;
                case FIFTY: System.out.println("$50"); break;
                case HUNDRED: System.out.println("$100"); break;
                default: break;
            }
        }  
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *