Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Struggling with if and if else

  1. #1
    Junior Member
    Join Date
    Oct 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default Struggling with if and if else

    Hello just arrived today so hello everyone
    I'm trying to get my head around if statments
    I have 6 items that i can buy (in my little shop) But I am only allowed to buy 3 of the 6
    I have the code worked out price, quantity ect
    but cant manage the ifs
    here is what i have so far without any ifs any help would be super it is causing me to dream in if's
    I am only starting out so I can only use if, if else and else

    import java.util.Scanner;
    class ApplesBanannas
    public static void main(String args[]) throws Exception

    double aPrice, appleCost;
    int aQuantity;
    aPrice = 0.30;

    Scanner in = new Scanner(System.in); // standard input is keyboard for console application,
    System.out.print("Enter quantity of apples required ");
    aQuantity = in.nextInt();
    appleCost = aPrice * aQuantity; //calculate final cost by adding tax
    String fs1 = String.format(" your apples will cost you: %.2f", appleCost);

    double oPrice, orangesCost;
    int oQuantity;
    oPrice = 0.45;

    System.out.print("Enter quantity of oranges required ");
    oQuantity = in.nextInt();
    orangesCost = oPrice * oQuantity; //calculate final cost by adding tax
    String fs2 = String.format(" your oranges will cost you: %.2f", orangesCost);

    double stPrice, straberriesCost;
    int stQuantity;
    stPrice = 2.30;

    System.out.print("Enter quantity of straberries required ");
    stQuantity = in.nextInt();
    straberriesCost = stPrice * stQuantity; //calculate final cost
    String fs3 = String.format(" your straberries will cost you: %.2f", straberriesCost);

    double pPrice, patatoCost;
    int pQuantity;
    pPrice = 3.25;

    System.out.print("Enter quantity of patatoes required ");
    pQuantity = in.nextInt();
    patatoCost = pPrice * pQuantity; //calculate final cost
    String fs4 = String.format(" your patatoes will cost you: %.2f", patatoCost);

    double tPrice, turnipCost;
    int tQuantity;
    tPrice = 0.70;

    System.out.print("Enter quantity of turnips required ");
    tQuantity = in.nextInt();
    turnipCost = tPrice * tQuantity; //calculate final cost
    String fs5 = String.format(" your turnips will cost you: %.2f", turnipCost);

    double cPrice, carrotCost;
    int cQuantity;
    cPrice = 1.25;

    System.out.print("Enter quantity of carrots required ");
    cQuantity = in.nextInt();
    carrotCost = cPrice * cQuantity; //calculate final cost
    String fs6 = String.format(" your carrots will cost you: %.2f", carrotCost);

    System.out.println (" \t\t your basket total comes to");
    System.out.println(+appleCost + carrotCost+ " Please have your money Ready to check out");

  2. #2
    Join Date
    Oct 2011
    My Mood
    Thanked 2 Times in 2 Posts

    Default Re: Struggling with if and if else

       sleep.dream("if statements");

    Your codes looking good, don't forget to put it into code tags to make it easier to read

    First of all, you will need to decide what the rules are, 3 different types, or 3 total? You will need to add another variable to keep track of this.
    Once you have done that, have a look at your logic and identify the places where your quantity could increase beyond 3. These are the places you will need to test the quantity.

    Buy products...
    Do I have more than 3?
    Buy products
    Do I have more than 3?

    For syntax, have a look at:
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Let me know how you go!

    P.S, Once you master if statements, have a look at loops. They will help with repeditive tasks, like buying 6 products:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Junior Member
    Join Date
    Oct 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default Re: Struggling with if and if else

    Understand just not sinking in. 2 more days will be bauld and probly need new laptop 2 days is long time so might end up expert within that time cheers for reply

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    My Mood
    Thanked 54 Times in 47 Posts
    Blog Entries

    Default Re: Struggling with if and if else

    Wrap you're code with highlight tags
    [highlight=Java] **The code goes here** [/highlight]

  5. #5
    Junior Member
    Join Date
    Oct 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default Re: Struggling with if and if else

    Think i have my head around it. I worked it around a simple if age is >21 made it so apples > 1 oranges > 2 straberrys> 3 using if and if else, else. Done it x3 times. 4 items in the last choice. So as i can have full selection.:-) not a normal way to do it but loops were not allowed to be used. Slow process just have to put all parts togetherin propper structure And do what most programmers do first. The psucodo code. Cheers for advise gave me fresh way to look at the problem at hand.

Similar Threads

  1. Struggling with Java :o
    By Jabdennel in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2011, 09:08 PM
  2. Hi from UK, Struggling with java!
    By Sneak in forum Member Introductions
    Replies: 6
    Last Post: December 14th, 2009, 09:39 AM
  3. Replies: 3
    Last Post: June 26th, 2009, 11:06 AM
  4. Details about 'CopyTo' of Arrays in Java
    By Fendaril in forum Collections and Generics
    Replies: 18
    Last Post: November 13th, 2008, 08:31 AM