simple addition inside { of after the if
good evening peoples
Once again I am on top of my head if anyone could point me in the right direction I would be very grateful.
As you can see from snippet of code below I can chooce 1 of the following items (i have to pick 3 items so have the below done 3 times it works I am not to use loops yet)
the problem is at the end of picking my products it should add up total. It dosent seem to do that without the ifs, if else it will. I have tryed putting the nested if I press 1 for apples then it just makes me take strawberries ..... thinking about this all day in work and its noe 4.45 am
Code Java:
System.out.print ("please select one of the following by pressing tne number on your keypad");
System.out.println();
System.out.println("1. Apples");
System.out.println("2. Oranges");
System.out.println("3. Straberries");
System.out.println("4. Patato");
System.out.println("5. Turnips");
System.out.println("6. Carrots");
fruit = in.nextInt();
if (fruit <=1)
{
System.out.println ("Super choice");
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);
}
else if (fruit <=2){
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);
}
else if (fruit <=3){
Re: simple addition inside { of after the if
this is what im getting
System.out.println(+appleCost + carrotCost+
simple addition
5 errors found:
File: C:\Users\
Error: variable appleCost might not have been initialized
File: C:\Users\
Error: variable carrotCost might not have been initialized
How do i initalize appleCost ect if its inside an if and it needs user input????
Re: simple addition inside { of after the if
The compiler does not know that you have given it a value, for all it know's your trying to do
Code Java:
int carrots;
if(1 == 2)
{
carrots = 2;
}
System.out.println(carrots);
, what is it suppose to print then?
To initialize it, give it some default value, -1 is popular
Code Java:
int appleCost = -1;
int carrotCost = -1
//if statement, get user input, set the variable