0

我对我的任务有这个想法,我希望收银系统在用户输入它的成本价格和所述项目的数量时计算项目的总数。

这似乎可行,但随后导致了我的主要问题 - 我想让用户在说 10 次交易之后输入字母“T”,以找出当天的总收入。

我尝试在计算等中使用带有 BigDecimal 数学类的 for 循环。我的计算中的“valueOf”一词有错误,Eclipse 一直试图将我的值更改为“long”,我很确定这是不对的.

我的解释并不令人惊讶,所以我会给你我写的代码,并在我的错误旁边放置注释..

 try{
    Scanner in = new Scanner (System.in);       
    String t = "T";
    int count;

    for (count = 1;count<=10;count++){
           System.out.println("\n\nValue of Item " + count + " :");
       BigDecimal itemPrice = in.nextBigDecimal();
       System.out.println("Quantity of item " + count + " :");
       BigDecimal itemQuantity = in.nextBigDecimal();
       BigDecimal itemTotal = (BigDecimal.valueOf(itemPrice).multiply // error here
                                  (BigDecimal.valueOf(itemQuantity))); // error here
           System.out.println("\nTotal for item(s): £" + itemTotal); 
       count++;

           while (t == "T"){
       BigDecimal amountOfItems = (BigDecimal.valueOf(itemTotal).divide // error here
                                      (BigDecimal.valueOf(itemQuantity))); // error here 
       BigDecimal totalTakings = (BigDecimal.valueOf(itemTotal).multiply // error here
                                     (BigDecimal.valueOf(amountOfItems))); // error here
       System.out.println("The Total Takings For Today is £" + totalTakings + " " );

     }
    }  
   }
  }
 }

就像我说的那样,eclipse 用来显示错误的“红线”仅在我的 BigDecimal 计算中的“valueOf”字样下方。

任何帮助都会很棒,因为我要把头发扯掉!!!!

谢谢,

维尼。

4

1 回答 1

2

没有办法BigDecimal.valueOf(BigDecimal)itemPrice并且已经itemQuantity是值 - 您不需要任何转换: BigDecimal

BigDecimal itemTotal = itemPrice.multiply(itemQuantity);

编辑:好的,以上解决了您的直接问题,但是您还得到了:

while (t == "T")
{
    // Loop that never changes the value of t
}

这有两个问题:

  • 循环要么永远执行,要么根本不执行,或者一直持续到抛出异常,因为循环条件永远不会改变,因为你没有改变t. 我猜你想t = System.in.readLine()在某个时候...
  • 您在这里比较两个字符串引用,而我怀疑您想比较它们的,例如

    while (t.equals("T"))
    
于 2011-08-05T15:18:15.590 回答