嘿伙计们,在过去的几个小时里,我一直在为课堂上研究这个程序,但似乎无法解决最后两个问题。它基本上是一个略微修改的CashRegister类,通过菜单具有基本功能。我遇到的问题是:
1(用户第一次选择菜单后,每次菜单都会在控制台中显示两次,我似乎找不到解决此问题的方法。
2(此外,每当我选择显示收银机的内容时,无论输入如何,第一行始终输出为0.00。
这是我的收银机课程,然后是我的测试人员:
import java.util.ArrayList;
/** * */
/** @author科尔 * */公共类收银机 {
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.ArrayList;
import java.util.Scanner;
/** * */
/** @author科尔 * */公共类 Prog2 {
/**
* @param args
*/
public static final String MENU = "******************************************n" +
"* Enter "n" to start a new Cash Register. *n" +
"* Enter "a" to add an item to the current Cash Register. *n" +
"* Enter "d" to display the total of the current Cash Register. *n" +
"* Enter "e" to exit the program. *n" +
"******************************************";
public static final String NEW_CUSTOMER = "n";
public static final String ADD_ITEM = "a";
public static final String DISPLAY = "d";
public static final String EXIT = "e";
public static void main(String[] args) {
// TODO Auto-generated method stub
CashRegister register = new CashRegister();
Scanner keyboard = new Scanner(System.in);
String input;
double userInput = 0;
do {
input = printMenu(keyboard);
if(input.equals(NEW_CUSTOMER)){
register.getDailyTotal();
register.clear();
}
else if(input.equals(ADD_ITEM)){
System.out.println("Please enter the price of the item: ");
register.addItem(userInput);
userInput = keyboard.nextDouble();
}
else if(input.equalsIgnoreCase(DISPLAY)){
register.display();
System.out.println("Total: " + register.getTotal());
System.out.println("Item Count: " +register.getCount());
}
else if(input.equalsIgnoreCase(EXIT)){
System.out.printf("Daily Sales Total: " + "%.2f%n",register.getDailyTotal());
System.out.println("Program Ended...");
break;
}
}while(input != EXIT);
}
public static String printMenu(Scanner input){ //this method displays the menu for the user
String response = "no reponse yet";
System.out.println(MENU);
response = input.nextLine();
return response; //response returned based on the users input
}
}
在添加项目之前,您需要从用户那里获得输入,这就是您为第一个项目获得 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);
}