gpt4 book ai didi

输入后显示两次的 Java 菜单

转载 作者:行者123 更新时间:2023-11-30 10:33:42 26 4
gpt4 key购买 nike

大家好,过去几个小时我一直在为类(class)编写这个程序,但似乎无法解决最后两个问题。它基本上是一个略微修改的 CashRegister 类,通过菜单提供基本功能。我遇到的问题是:

1) 用户第一次选择菜单后,每次菜单在控制台中出现两次,我似乎找不到解决办法。

2) 此外,每当我选择显示我的 CashRegister 的内容时,无论输入如何,第一行始终输出为 0.00。

这是我的 CashRegister 类,然后是我的测试人员:

import java.util.ArrayList;

/** * */

/** * @作者科尔 * */公共(public)类 CashRegister {

   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;

导入java.util.Scanner;

/** * */

/** * @作者科尔 * */公开课 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);
}

关于输入后显示两次的 Java 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com