can someone help me do the following with my java code, so that it compiles, and
ID: 2239502 • Letter: C
Question
can someone help me do the following with my java code, so that it compiles, and it adds the product up, and returns a total?
public class Clientlist {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int product = 0;
int apples = 0;
int oranges = 0;
int strawberries = 0;
int bananas = 0;
int potatoes = 0;
int tomatoes = 0;
int lemons = 0;
int lettuce = 0;
int cucumbers = 0;
int celery = 0;
int amount = 0;
int total = celery + cucumbers + lettuce + lemons + tomatoes +
potatoes + bananas + strawberries + oranges + apples;
while (product != 99) {
System.out.print("99- quit, 1- Apples, " +
"2- Oranges, 3- Strawberries, " +
"4- Bananas, 5- Potatoes, " +
"6- Tomatoes, 7- Lemons, " +
"8- Lettuce, 9- Cucumbers, " +
"10- Celery ");
product = input.nextInt();
if (product == 99)
System.out.println("Your total is: " + total);
else
System.out.print("Enter amount: ");
amount = input.nextInt();
switch (product){
case 1: apples = (int) (amount * 0.25);
break;
case 2: oranges = (int) (amount * 0.12);
break;
case 3: strawberries = (int) (amount * 0.30);
break;
case 4: bananas = (int) (amount * 0.10);
break;
case 5: potatoes = (int) (amount * 2.50);
break;
case 6: tomatoes = amount * 1;
break;
case 7: lemons = (int) (amount * 0.5);
break;
case 8: lettuce = (int) (amount * 0.30);
break;
case 9: cucumbers = (int) (amount * 0.18);
break;
case 10: celery = (int) (amount * 1.20);
break;
}
}
}
}
Explanation / Answer
Please rate with 5 stars :) You are perfectly on the right track, you just have missed summing the entire thing up inside the loop.
Here is what you have to do :)
public class Clientlist {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int product = 0;
int apples = 0;
int oranges = 0;
int strawberries = 0;
int bananas = 0;
int potatoes = 0;
int tomatoes = 0;
int lemons = 0;
int lettuce = 0;
int cucumbers = 0;
int celery = 0;
int amount = 0;
int total = celery + cucumbers + lettuce + lemons + tomatoes +
potatoes + bananas + strawberries + oranges + apples;
while (product != 99) {
total = celery + cucumbers + lettuce + lemons + tomatoes +
potatoes + bananas + strawberries + oranges + apples;
System.out.print("99- quit, 1- Apples, " +
"2- Oranges, 3- Strawberries, " +
"4- Bananas, 5- Potatoes, " +
"6- Tomatoes, 7- Lemons, " +
"8- Lettuce, 9- Cucumbers, " +
"10- Celery ");
product = input.nextInt();
if (product == 99)
System.out.println("Your total is: " + total);
else
System.out.print("Enter amount: ");
amount = input.nextInt();
switch (product){
case 1: apples = (int) (amount * 0.25);
break;
case 2: oranges = (int) (amount * 0.12);
break;
case 3: strawberries = (int) (amount * 0.30);
break;
case 4: bananas = (int) (amount * 0.10);
break;
case 5: potatoes = (int) (amount * 2.50);
break;
case 6: tomatoes = amount * 1;
break;
case 7: lemons = (int) (amount * 0.5);
break;
case 8: lettuce = (int) (amount * 0.30);
break;
case 9: cucumbers = (int) (amount * 0.18);
break;
case 10: celery = (int) (amount * 1.20);
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.