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.

>> REGISTER NOW TO START POSTING

# Thread: Struggling with if and if else

1. ## 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);
System.out.println(fs1);

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);
System.out.println(fs2);

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);
System.out.println(fs3);

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);
System.out.println(fs4);

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);
System.out.println(fs5);

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(fs6);

}
}

2. ## Re: Struggling with if and if else

```if(!rested){
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.

e.g:
Do I have more than 3?
No?
Do I have more than 3?
etc...

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. ## 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. ## Re: Struggling with if and if else

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

5. ## 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.