2014-02-10 20 views
-2

我继续为我的输出获取0.0。我不知道我做错了什么。任何帮助,将不胜感激。获取0代替预期计算的输出

我前几部分的用户输入正常,并且数字正确显示。它只是要求总量和随后的数学,我得到的0.0,0.0的时候,0.0

import java.util.Scanner; 

public class CoffeeShop { 
    public static void main(String[] args) { 

     //Welcome Message and name prompt 
     String username; 

     System.out.println("What is your name?"); 
     Scanner keyboard = new Scanner(System.in); 
     username = keyboard.next(); 
     System.out.println("Welcome to the Java Byte Code Coffee Shop," + username + "!"); 

     //Menus 
     System.out.println("Here is our menu."); 
     System.out.println("1. Coffee $1.50"); 
     System.out.println("2. Latte $3.50"); 
     System.out.println("3. Cappuccino $3.25"); 
     System.out.println("4. Espresso $2.00"); 

     //What item do they want? 
     int product_Number; 
     System.out.println("Please enter the item number."); 
     Scanner item = new Scanner(System.in); 
     product_Number = item.nextInt(); 
     System.out.println("You selected item " + product_Number); 
     double product; 

     if (product_Number == 1) { 
      product = 1.50; 
     } 
     if (product_Number == 2) { 
      product = 3.50; 
     } 
     if (product_Number == 3) { 
      product = 3.25; 
     } 
     if (product_Number == 4) { 
      product = 2.00; 
     } else { 
      product = 0.00; 
     } 

     //Quantity of item 
     int quantity; 
     System.out.println("How many would you like?"); 
     Scanner amount = new Scanner(System.in); 
     quantity = amount.nextInt(); 
     System.out.println("You want " + quantity + " of them!"); 
     //Testing Product 
     double total = quantity * product; 
     System.out.println("Total before discount and tax is " + total); 

     double nuTotal; 
     //Discount/Tax 
     if (total >= 10) { 
      nuTotal = total - (total * .1); 
     } else { 
      nuTotal = total; 
     } 
     System.out.println("Your total with discount is " + nuTotal); 
     double totalTax = nuTotal * .07; 
     System.out.println("Your total with tax is " + totalTax); 

     System.out.println("Thank you " + username + "! Please stop by again!"); 

    } 
} 
+1

所以把更多的调试输出,并追查到底哪一行是“丢失”你的值... –

+1

请发布质量好的代码 - 这种压痕伤害眼睛! –

+0

@kittyPL:修正了缩进。使用IntelliJ或任何自动格式化IDE并不困难,因此花两分钟做这件事有什么坏处? – Makoto

回答

5

if语句被分解 - 最后一条语句总是*保证火,给你0.0

你应该集合在一起为if-else if-else声明,因为这样的:

if (product_Number == 1) { 
    product = 1.50; 
} else if (product_Number == 2) { 
    product = 3.50; 
} else if (product_Number == 3) { 
    product = 3.25; 
} else if (product_Number == 4) { 
    product = 2.00; 
} else { 
    product = 0.00; 
} 

而且,你的总说法是错误的,太 - 你只打印出tax值,而不是total + tax值。

*:如果您选择了4,那么它实际上会工作。但那是因为4的选择附加到else

0

你也可以去像这样:

double product = 0; 

    if (product_Number == 1) { 
     product = 1.50; 
    } 
    if (product_Number == 2) { 
     product = 3.50; 
    } 
    if (product_Number == 3) { 
     product = 3.25; 
    } 
    if (product_Number == 4) { 
     product = 2.00; 
    } 

赋予变量产品价值零(0),然后给它一个新的价值进一步下跌的代码。

+1

此时,您现在正在检查每个可能的块,而不管该值是否设置。比if-else if-else,甚至'switch'好一点。 – Makoto