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){
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????
My apologies 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 choose 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 doesn?t seem to do that without the ifs, if else it will. I have tried 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 now 4.45 am
import java.util.*;
class Example1{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
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. Potato");
System.out.println("5. Turnips");
System.out.println("6. Carrots");
int fruit=0,quantity=0,totalcost=0;
fruit = input.nextInt();
if (fruit ==1){
System.out.println ("Super choice");
System.out.print("Enter quantity of apples required: ");
quantity = input.nextInt();
int price=100;
totalcost = price * quantity;
}
if (fruit ==2){
System.out.print("Enter quantity of oranges required: ");
quantity = input.nextInt();
int price=50;
totalcost = price * quantity;
}
if (fruit ==3){
System.out.print("Enter quantity of strawberries required: ");
quantity = input.nextInt();
int price=200;
totalcost = price * quantity;
}
if (fruit ==4){
System.out.print("Enter quantity of potatoes required: ");
quantity = input.nextInt();
int price=10;
totalcost = price * quantity;
}
if (fruit ==5){
System.out.print("Enter quantity of apples required: ");
quantity = input.nextInt();
int price=25;
totalcost = price * quantity;
}
if (fruit ==6){
System.out.print("Enter quantity of apples required: ");
quantity = input.nextInt();
int price=20;
totalcost = price * quantity;
}
System.out.println("Total Cost: "+totalcost);
}
}
Tks for reply eventually got it to work out Not as neetly but it worked. i dont know if i should start loving this java of hate it.
System.out.print("Enter quantity of turnips required ");
Quantity = in.nextInt(); basket3 = tPrice * Quantity; //calculate final cost String fs1 = String.format(" your turnips will cost you: %.2f", +basket3); System.out.println(fs1);}
basket = basket1+basket2+basket3; System.out.println (" your basket total comes to $"+basket); if (basket <19.99 ) { System.out.println("no discount today"); yourdiscount = basket*discount; total = basket-yourdiscount;