Results 1 to 2 of 2
Thread: Help! Cant figure it out
- 09-12-2013, 04:08 AM #1
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Help! Cant figure it out
So I have a homework program almost finished but I cannot figure out for the life of why this condition will not compute. Basically if the hourUsage is over 10 but under 21 it should compute the following, but it doesnt:
else if (hourUsage > 10 && hourUsage < 21)
{
additionalHours = (hourUsage - 10);
totalCost = packA + (additionalHours * 2.00);
savingsAB = totalCost - packB;
if (savingsAB > 0)
System.out.printf("You would save $%,.2f if you" +
" purchase Package B.\n", savingsAB);
}
else
{
additionalHours = (hourUsage - 10);
totalCost = packA + (additionalHours * 2.00);
savingsAC = totalCost - packC;
if (savingsAC > 0)
System.out.printf("You would save $%,.2f if you" +
" purchase Package C.\n", savingsAC);
}
in case 'a' will not compute.
Heres the homework question:
Question: Internet Service Provider
An Internet service provider has three different subscription packages for its customers:
Package A: $9.95 per month for 10 hours access. Additional hours are $2.00 per hour
Package B: $13.95 per month for 20 hours access. Additional hours are $1.00 per hour
Package C: $29.99 per month for unlimited access.
Write a program that calculates a customer's monthly bill. The program should ask the user to enter the letter of the package the customer purchased (A, or B, or C) and the number of hours that we used. It should calculate and display the total charges.
The program should also calculate and display the amount of money Package A customers would save if they purchased Package B or C, and the amount of money Package B customers would save if they purchased Package C. If there would be no savings, no message should be displayed.
And heres my code:
import java.util.Scanner;
public class InternetService
{
public static void main(String[] args)
{
//Variable Declaration
String input;
double hourUsage, additionalHours, totalCost,
savingsAB, savingsAC, savingsBC;
char packLetter;
final double packA = 9.95;
final double packB = 13.95;
final double packC = 29.99;
Scanner userInput = new Scanner(System.in);
//Promt user for Input
System.out.print("Enter the letter of the package" +
"you have: ");
input = userInput.nextLine();
packLetter = input.charAt(0);
System.out.print("Enter the amount of hours used: ");
hourUsage = userInput.nextDouble();
//Case Structure
switch (packLetter)
{
case 'a':
case 'A':
if (hourUsage <= 10)
{
System.out.print("Your monthly bill is $" + packA);
}
else if (hourUsage > 10 && hourUsage < 21)
{
additionalHours = (hourUsage - 10);
totalCost = packA + (additionalHours * 2.00);
savingsAB = totalCost - packB;
if (savingsAB > 0)
System.out.printf("You would save $%,.2f if you" +
" purchase Package B.\n", savingsAB);
}
else
{
additionalHours = (hourUsage - 10);
totalCost = packA + (additionalHours * 2.00);
savingsAC = totalCost - packC;
if (savingsAC > 0)
System.out.printf("You would save $%,.2f if you" +
" purchase Package C.\n", savingsAC);
}
break;
case 'b':
case 'B':
if (hourUsage <= 20)
{
System.out.print("Your monthly bill is $" + packB);
}
else if (hourUsage > 20)
{
additionalHours = (hourUsage - 20);
totalCost = packB + (additionalHours * 1.00);
savingsBC = totalCost - packC;
System.out.printf("Your monthly bill is $%,.2f\n", totalCost);
if (savingsBC > 0)
System.out.printf("You would save $%,.2f\n if you" +
" purchase package C.\n", savingsBC);
}
break;
case 'c':
case 'C':
System.out.print("Your monthly bill is $" + packC);
break;
default:
System.out.println("Invalid choice.");
}
}
}
- 09-12-2013, 09:13 AM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Help! Cant figure it out
Yes it does. So if it doesn't do what you expect it to do, your expectations are wrong. hourUsage is not between 10 and 21.
I am making assumptions here because I don't want to study that blurb of unformatted code. If you want better help, learn how to use this forum properly:
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum - Learn Java ProgrammingLast edited by gimbal2; 09-12-2013 at 09:16 AM.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
Please me figure this out
By atrodeyo in forum New To JavaReplies: 7Last Post: 04-30-2012, 02:14 PM -
can't figure it out myself
By Doyle Raymond in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-18-2011, 03:34 PM -
can someone help me to figure this out >.<
By kissmeeq in forum New To JavaReplies: 2Last Post: 08-01-2011, 03:19 PM -
Need help - I can't figure it out.
By Joshsmith in forum New To JavaReplies: 2Last Post: 10-23-2009, 10:12 PM -
I can't figure this out
By silvia in forum New To JavaReplies: 3Last Post: 07-20-2007, 04:38 AM
Bookmarks