6 Replies - 389 Views - Last Post: 13 May 2014 - 01:14 PM Rate Topic: -----

#1 StoneyFL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

Java Simple program

Posted 12 May 2014 - 03:59 PM

Hello everyone, I am new to java and this is one of my first program assignments. I can't figure out how to make the if else statements work. I am trying to get the input from the user to be multiplied by the package cost but it gives me an error. I also can't figure out how to solve for the discounted total. If anyone could figure this out for it would help so much, thanks!

Purpose of the program:
To write a program that asks the user to enter the number of packages purchased. The program should then display the amount without any discount, the amount of the calculated discount (if any), and the total amount of the purchase after the discount is applied.

What the output should be:
Enter the number of packages purchased: 10
Your total without discount: $990.00
The discount is: $198.00
The discounted total: $792.00


import java.util.Scanner;

public class Practice {

	public static void main(String[] args) {
		double packageCost = 99.00;
		double amtPackage = 10.00;
		double withoutDiscount = packageCost * (double) amtPackage;
		double theDiscountTotal =withoutDiscount - (packageCost * amtPackage);
		
		double twentyPercent = withoutDiscount * 0.20;
		double thirtyThreePercent = withoutDiscount * 0.33;
		double fortyTwoPercent = withoutDiscount * 0.42;
		double fortyNinePercent = withoutDiscount * 0.49;
		
		Scanner user_input = new Scanner(System.in);
		System.out.print("Enter the number of packages purchased: " );
		int askPackage = user_input.nextInt();;
 	
		System.out.println("Your total without discount: " + (double) withoutDiscount);
		
		if (askPackage >= 10 || askPackage <= 19) {
		System.out.println("The discount is: " + twentyPercent);
		
		}else if
			(askPackage >= 20 || askPackage <= 49) {
		System.out.println("The discount is: " + thirtyThreePercent);
		
		}else if
			(askPackage >= 50 || askPackage <= 99) {
		System.out.println("The discount is: " + fortyTwoPercent);
		}else if
			(askPackage >= 100 || askPackage > 100) {
		System.out.println("The discount is: " + fortyNinePercent);
		}
		
		System.out.println("The discounted total is: " + theDiscountTotal);
	}
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java Simple program

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 802
  • View blog
  • Posts: 3,510
  • Joined: 03-December 12

Re: Java Simple program

Posted 12 May 2014 - 04:39 PM

When asking for help we also need to know what is happening. IE, what are you expecting compared to what it is doing; Any errors received, ect. We are not looking over your shoulder so we can only go off of what you tell us is happening... That said,


Quote

08 double withoutDiscount = packageCost * (double) amtPackage;
09 double theDiscountTotal =withoutDiscount - (packageCost * amtPackage);
15
16 Scanner user_input = new Scanner(System.in);
17 System.out.print("Enter the number of packages purchased: " );
18 int askPackage = user_input.nextInt();;
19
20 System.out.println("Your total without discount: " + (double) withoutDiscount);


I am guessing this is one problem area?

When initialied your withoutDiscount has a value of 0. So, when you print your statement on line 20, either an error is given or the output is 0?

Do your math AFTER you get the values, the only way it works differently is when used in a function and I doubt you are to that point yet. You seem to have the same issues in your IF statements.


Get your input before you do the math on that input. Then you will have valid output.
Was This Post Helpful? 1
  • +
  • -

#3 StoneyFL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

Re: Java Simple program

Posted 12 May 2014 - 09:24 PM

Yea my bad there was never an error sorry for that one, I just needed the user input to change after entering say "19 packages" but it would still give the same output. I think this is because I set amtPackages to 10 but am really not sure how to go about this.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 802
  • View blog
  • Posts: 3,510
  • Joined: 03-December 12

Re: Java Simple program

Posted 12 May 2014 - 11:46 PM

All of the math used, uses amtPackage I.stead of askPackage. Change that variable name when you do the math I. The new location and it will solve that problem.
Was This Post Helpful? 1
  • +
  • -

#5 StoneyFL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

Re: Java Simple program

Posted 13 May 2014 - 11:46 AM

Thanks for the reply.

So I fixed my code a little and the only problem I am having with it now is that it is not giving me the correct output. My teacher wants the results for 58, but I am getting the wrong answer when asking for the discount.

My output:
Enter the number of packages purchased: 58
Your total without discount: 5742.0
The discount is: 1148.4
The discounted total: 4593.6

What it should have:
Enter the number of packages purchased: 58
Your total without discount: 5742.0
The discount is: 2411.6
The discounted total: 3330.3

import java.util.Scanner;

public class Practice {

	public static void main(String[] args) {
		//declare variable
		double packageCost = 99.00;
	
		//get user input
		Scanner user_input = new Scanner(System.in);
		System.out.print("Enter the number of packages purchased: " );
		int askPackage = user_input.nextInt();
		
		//math for total discount and discounted result
		double withoutDiscount = packageCost * askPackage;
		double firstDiscount = (packageCost * askPackage) - (withoutDiscount * .20);
		double secondDiscount = (packageCost * askPackage) - (withoutDiscount * .33);
		double thirdDiscount = (packageCost * askPackage) - (withoutDiscount * .42);
		double fourthDiscount = (packageCost * askPackage) - (withoutDiscount * .49);
		
		//print total without discount
		System.out.println("Your total without discount: " + withoutDiscount);
		
		//if else statements and print line
		if (askPackage >= 10 || askPackage <= 19) {
		System.out.println("The discount is: " + withoutDiscount * .20);
		System.out.println("The discounted total: " + firstDiscount);
		
		}else if
			(askPackage >= 20 || askPackage <= 49) {
		System.out.println("The discount is: " + withoutDiscount * .33);
		System.out.println("The discounted total: " + secondDiscount);
		
		}else if
			(askPackage >= 50 || askPackage <= 99) {
		System.out.println("The discount is: " + withoutDiscount * .42);
		System.out.println("The discounted total: " + thirdDiscount);
		
		}else if
			(askPackage >= 100) {
		System.out.println("The discount is: " + withoutDiscount * .49);
		System.out.println("The discounted total: " + fourthDiscount);
		}

	}
	
}

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 802
  • View blog
  • Posts: 3,510
  • Joined: 03-December 12

Re: Java Simple program

Posted 13 May 2014 - 12:15 PM

Your math equals what it should. Your problem comes from the if statements. They are not meaning what you think they mean.

25	        if (askPackage >= 10 || askPackage <= 19) {

Your math comes up with stopping at the first if statement.

This statement means:

if (58 is greater than or equal to 10)
   OR 
   (58 is less than equal to 19)


Is 58 greater than or equal to 10?
If yes, this statement is true and do not evaluate the next other if statement.

Look at your comparison operators again for clarity.

Spoiler

This post has been edited by astonecipher: 13 May 2014 - 12:16 PM

Was This Post Helpful? 1
  • +
  • -

#7 StoneyFL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-May 14

Re: Java Simple program

Posted 13 May 2014 - 01:14 PM

Ahh man I should have used the double ampersand, rookie move on my behalf... thanks man
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1