Welll there are a couple things you can do
input is a string right? That means you can just do
Or you can make it ignore the case (because it could mess up if you check for "A" but the user puts "a"
java Code:
if (input.equalsIgnoreCase("A"))
This means it will accept a or A. Your teacher or professor may like this better
java Code:
import javax.swing.JOptionPane;/*Andrew Varnick
9/23/2013
CSCI 1015
Programming Assignment 4
A program to calculate a monthly bill
*/public class VarnickPass4
{//Constantsstatic final double PackA
= 9.95; static final double PackB
= 13.95;static final double PackC
= 19.95; public static void main
(String[] args
) { String input
; String numberOfHoursUsed
; int numberOfHours
; int packageInput
; double finalPrice
; double PackA
= 9.95; double PackB
= 13.95; double PackC
= 19.95; input
= JOptionPane.
showInputDialog("Which package do you have?(A = 1, B = 2 or C= 3)"); // packageInput = Integer.parseInt(input); if (input.
equalsIgnoreCase("A")) { numberOfHoursUsed
= JOptionPane.
showInputDialog("How many hours have you used this month?"); numberOfHours
= Integer.
parseInt(numberOfHoursUsed
); finalPrice
= ((numberOfHours
- 10) * 2 + PackA
); JOptionPane.
showMessageDialog(null,
"The final price is $" + finalPrice
); } else { if (input.
equalsIgnoreCase("B")) { numberOfHoursUsed
= JOptionPane.
showInputDialog("How many hours have you used this month?"); numberOfHours
= Integer.
parseInt(numberOfHoursUsed
); finalPrice
= ((numberOfHours
- 20) * 1 + PackB
); JOptionPane.
showMessageDialog(null,
"The final price is $" + finalPrice
); } else { if (input.
equalsIgnoreCase("C")) numberOfHoursUsed
= JOptionPane.
showInputDialog("You have unlimated access!"); finalPrice
= (PackC
); JOptionPane.
showMessageDialog(null,
"The final price is $" + finalPrice
); } } }}
Or you could use the switch statement (but I am unsure how to use or if you can use IgnoreCase with this)
java Code:
switch (input) {
case "A": {
// blah blah blah
break;
}
case "B": {
// blah blah blah
break;
}
case "C": {
// blah blah blah
break;
}
}
After each case you will want to break since the switch statement is a loop(someone correct me if I'm wrong) If you don't break it wouldn't mess up your program but it would keep trying the next cases if there were any (so if input == a it would do that statement and then check if input is b and then c unless you break.. so in your case if you don't break the program will be like .0000006 ms slower but it's good practice)