嘿伙计们,过去几个小时我一直在上课这个课程,似乎无法解决最后两个问题。它基本上是一个稍微修改过的CashRegister类,通过菜单提供基本功能。我遇到的问题是:
1)用户第一次进行菜单选择后,菜单每次都会在控制台中显示两次,我似乎无法找到解决方法。
2)每当我选择显示CashRegister的内容时,无论输入如何,第一行总是输出为0.00。
这是我的CashRegister课程,后面是我的测试人员:
=IIF((DateDiff("d",Today(),Fields!Started.Value)>=3 and (Fields!Started.Value) <= 6), AND (=Fields!NumSuccess.Value)=4 "Yellow",
IIF(DateDiff("d",Today(),Fields!Started.Value)>= 7, AND (=Fields!NumSuccess.Value)!=4, "Red",
true,"DarkGreen"))
/ ** * * /
/ ** * @author Cole * * / 公共类CashRegister {
import java.util.ArrayList;
}
private double dailyTotal;
private double totalPrice;
ArrayList<Double> items;
/**
Constructs a cash register with cleared item count and total.
*/
public CashRegister()
{
items = new ArrayList<Double>();
dailyTotal = 0;
totalPrice= 0;
}
/**
Adds an item to this cash register.
@param price the price of this item
*/
public void addItem(double price)
{
items.add(price);
dailyTotal = dailyTotal + price;
}
/**
Gets the price of all items in the current sale.
@return the total amount
*/
public double getTotal()
{
for(int x=0; x<items.size(); x++){
totalPrice = totalPrice + items.get(x);
}
return totalPrice;
}
/**
Gets the number of items in the current sale.
@return the item count
*/
public int getCount()
{
return items.size();
}
/**
Clears the item count and the total.
*/
public void clear()
{
items.clear();
totalPrice = 0;
}
public void display(){
for(int x=0; x<items.size(); x++){
System.out.printf("%10.2f%n", items.get(x));
}
System.out.println("------------------------------");
}
public double getDailyTotal(){
dailyTotal = dailyTotal + totalPrice;
return dailyTotal;
}
import java.util.Scanner;
/ ** * * /
/ ** * @author Cole * * / 公共课Prog2 {
import java.util.ArrayList;
}
答案 0 :(得分:1)
您需要在添加项目之前获得用户的输入,这就是为什么您的第一个项目获得0的原因。由于userInput的值在开头设置为0并且您的语句被切换,因此您将始终首先创建一个值为0.0的项目,并且所有其他值将比实际输入落后一步。
else if(input.equals(ADD_ITEM)){
System.out.println("Please enter the price of the item: ");
userInput = keyboard.nextDouble();
register.addItem(userInput);
}