How to assign a total to a name in Java
I am using a switch statement to choose 5 products. I want to assign the total to a name I can add up
double total;
(prod1 * 12.80) = total1
problem is it says "total" is not initalized
I got prod1 * 12.80
to work, but I need the total of that computation to add to a statement like
System.out.println("grand total = " + total1 + total2 + " = " ;
All,
Here is the first part of my code switch. I have 4 more cases to the switch that all have totals. I need all thes开发者_运维知识库e totals from each switch to be added up for a grand total.
public class test
{
public static void main(String args[])
{
//declare a scanner for user Input
Scanner userInput = new Scanner(System.in);
int choice;
double prod1,prod2,prod3,prod4,prod5;
double lineCost1 = 2.98;
double lineCost2 = 4.50;
double total1 = prod1 * lineCost1; // This is a intial set valueforthe Grand total but can't get it to work
do
{
//display our menu
System.out.println("***ProductSelection***\n");
System.out.println("Please Select a Product Number from 1-5\n");
System.out.println("To quit the Program Please enter 6.");
System.out.println("*********************");
System.out.println("Please enter your choice:");
//get user input
choice = userInput.nextInt();
//switch the choice from user
switch(choice)
{
case 1://Product 1
System.out.println("Product 1");
System.out.println("Please enter the Quantity you want:");
prod1 = userInput.nextInt();
System.out.println("Quantity = " + prod1 + " at a Line Cost of " +"$"+ 2.98 + " = " +"$"+ (total1));
break;
THIS is THE ERROR I GET : variable prod1 might not have been initialized double total1 = prod1 * lineCost1; // This is a intial set valueforthe Grand total but can't get it to work ^ 1 error
What is wrong with the following? Why do you want to write your expression backwards?
double total = prod1 * 12.8;
精彩评论