Question
(Java Programming) so this the program I wrote last week public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println(What type of gas?);
(Java Programming)
so this the program I wrote last week
public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("What type of gas?"); System.out.println("(R)egular: $2.19"); System.out.println("(P)lus: $2.49"); System.out.println("(S)uper: $2.71"); System.out.println("(U)ltra: $2.99"); char option = keyboard.next().charAt(0); double rate ; if (option == 'R') rate =2.19; else if (option == 'P') rate = 2.49; else if (option == 'S') rate = 2.71; else rate = 2.99; System.out.println("How many gallons?"); double gallons = keyboard.nextDouble(); System.out.println("You owe :" + rate*gallons); } }
Now i just want to replace the if statement with switch statement
Modify the program you wrote last week by removing the if statement and replacing it with a switch statement. The execution of the program will be the same, the only change will be the decision structure.
If your program had the user entering numbers, you can switch based on numbers. If your program had the user entering letters, you can switch on characters. You can decide whether to let the user enter either upper-case and lower-case letters or require them to enter only upper-case or only lower-case. Although Java now allows it, please dont switch based on strings.
Format the dollar amounts properly using printf. The dollar amounts should have two numbers after the decimal point.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started