Friday, June 20, 2014

Java Exercise Nr. 33 (Selections)

A shipping company uses the following function to calculate the cost (in dollars) of shipping based on the weight of the package (in pounds).



Write a program that prompts the user to enter the weight of the package and display the shipping cost. If the weight is greater than 20, display a message “the package cannot be shipped.”

Solution:

public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   double weight;

   System.out.print("Enter the weight of the package:");
   weight = s.nextDouble();

   if(weight <= 0) {
System.out.print("You did not enter a valid weight.");
   } else {
if(weight <= 1) {
System.out.print("Your shipping cost is $3.5.");
} else if(weight <= 3) {
System.out.print("Your shipping cost is $5.5.");
} else if(weight <= 10) {
System.out.print("Your shipping cost is $8.5.");
} else if(weight <= 20) {
System.out.print("Your shipping cost is $10.5.");
} else {
System.out.print("The package cannot be shipped.");
}
   }
}

No comments:

Post a Comment

Author