I am having a problem with an assignment and need some help....
I get everything to compile, but it doesn't add up at the end. We have to put in miles and pounds. The weight works, but it always says $0.00 for the shipping price.
I'm searching right now to see how to copy my code to post it here. This is my first class in programming and I don't know how to copy the entire code and paste it....It you can tell me, great!
Thanks!
John
Shipping program not computing
Page 1 of 112 Replies - 309 Views - Last Post: 08 October 2012 - 06:18 PM
Replies To: Shipping program not computing
#2
Re: Shipping program not computing
Posted 08 October 2012 - 05:27 PM
Please post your code using code tags:
.
#3
Re: Shipping program not computing
Posted 08 October 2012 - 05:28 PM
Just highlight your code, and paste it into here. When you paste it in here, highlight it again in the reply area and click on the code button.
#4
Re: Shipping program not computing
Posted 08 October 2012 - 05:35 PM
import java.util.Scanner;
public class jlandauer_Shipping
{
public static void main ( String args[] )
{
System.out.println("Welcome to the You Package it We Savage It Shipping Company.");
System.out.println();
Scanner input = new Scanner(System.in);
int miles;
int pounds;
double rateCharged = 0;
int Segmiles = 0;
double Charge = (Segmiles*rateCharged);
miles = 1;
do
{
System.out.print ("How far will you be shipping your package in miles? ");
miles = input.nextInt();
}
while ( miles < 1 );
System.out.println();
pounds = 1;
do
{
System.out.print ("How heavy is your package in pounds (1-70)? ");
pounds = input.nextInt();
}
while ( pounds <= 0 || pounds > 70 );
{
if ( pounds >= 46 )
{
rateCharged = 8.89;
}
else if ( pounds >= 26 )
{
rateCharged = 7.63;
}
else if ( pounds >= 11 )
{
rateCharged = 5.81;
}
else if ( pounds >= 6 )
{
rateCharged = 4.42;
}
else
{
rateCharged = 2.67;
}
}
if ( miles % 25 == 0 )
{
Segmiles = (miles/25);
System.out.printf("\nThe shipping rate is %f per 25 miles. \nYour total shipping cost for %d miles is $%.2f\n", rateCharged, miles, Charge);
}
else
Segmiles = ((miles/25) + 1);
System.out.printf("\nThe shipping rate is %f per 25 miles. \nYour total shipping cost for %d miles is $%.2f\n", rateCharged, miles, Charge);
{
}
}
}
#5
Re: Shipping program not computing
Posted 08 October 2012 - 05:36 PM
See this line double Charge = (Segmiles*rateCharged);. You want to handle this calculation at the end, after all the variables have values that aren't 0.
#6
Re: Shipping program not computing
Posted 08 October 2012 - 05:39 PM
This is what I get when I compile the program. Everything enters fine, but the shipping amount is always $0.00.
Welcome to the You Package it We Savage It Shipping Company.
How far will you be shipping your package in miles? 5
How heavy is your package in pounds (1-70)? 5
The shipping rate is 2.670000 per 25 miles.
Your total shipping cost for 5 miles is $0.00
So just move it to after my last else statement? I'll see what it does. I guess I don't see why it needs to be moved.
Welcome to the You Package it We Savage It Shipping Company.
How far will you be shipping your package in miles? 5
How heavy is your package in pounds (1-70)? 5
The shipping rate is 2.670000 per 25 miles.
Your total shipping cost for 5 miles is $0.00
macosxnerd101, on 08 October 2012 - 05:36 PM, said:
See this line double Charge = (Segmiles*rateCharged);. You want to handle this calculation at the end, after all the variables have values that aren't 0.
So just move it to after my last else statement? I'll see what it does. I guess I don't see why it needs to be moved.
#7
Re: Shipping program not computing
Posted 08 October 2012 - 05:41 PM
When you initialize Charge, Segmiles = 0. After Segmiles has a non-zero value, then you want to calculate the Charge.
#8
Re: Shipping program not computing
Posted 08 October 2012 - 05:49 PM
macosxnerd101, on 08 October 2012 - 05:41 PM, said:
When you initialize Charge, Segmiles = 0. After Segmiles has a non-zero value, then you want to calculate the Charge.
OK...here's what I did.
double Charge = 0; //instead of segmiles*rateCharged
I still get the same result after I do that. Is there something wrong in lines 62 or 66? That's where I think the problem is, I just don't know where.
#9
Re: Shipping program not computing
Posted 08 October 2012 - 05:53 PM
Your program shouldnt even compile. You have some rogue braces on lines 67 and 68. I presume these should be for the else statement, so place your code inside of these braces.
This post has been edited by nick2price: 08 October 2012 - 05:53 PM
#10
Re: Shipping program not computing
Posted 08 October 2012 - 05:53 PM
OK...figured it out...just a little trial and error!
Also, why are there 4 zeroes in my output for the shipping rate? Should be 2.67, it's 2.670000
Also, why are there 4 zeroes in my output for the shipping rate? Should be 2.67, it's 2.670000
#11
Re: Shipping program not computing
Posted 08 October 2012 - 05:55 PM
You can use a System.out.printf() statement to round to two decimal places:
System.out.printf("Your shipping cost is %0.2f\n", Charge);
#12
Re: Shipping program not computing
Posted 08 October 2012 - 06:11 PM
#13
Re: Shipping program not computing
Posted 08 October 2012 - 06:18 PM
Glad I could help!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|