Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java菜单在输入后显示两次_Java_Menu - Fatal编程技术网

Java菜单在输入后显示两次

Java菜单在输入后显示两次,java,menu,Java,Menu,嘿,伙计们,在过去的几个小时里,我一直在做这个课程,但似乎无法解决最后两个问题。它基本上是一个稍加修改的收银机类,通过菜单实现基本功能。我遇到的问题是: 1) 在用户第一次选择菜单后,以后每次该菜单在控制台中出现两次,我似乎都找不到解决方法 2) 此外,每当我选择显示收银机的内容时,无论输入如何,第一行始终输出为0.00 下面是我的收银机课程,后面是我的测试人员: import java.util.ArrayList; /** * */ /** *@作者科尔 * */ 公营收银机{ p

嘿,伙计们,在过去的几个小时里,我一直在做这个课程,但似乎无法解决最后两个问题。它基本上是一个稍加修改的收银机类,通过菜单实现基本功能。我遇到的问题是:

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

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

下面是我的收银机课程,后面是我的测试人员:

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;


   }
导入java.util.Scanner

/** * */

/** *@作者科尔 * */ 公共类程序2{

/**
 * @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);    
}

你能告诉我们导致menj出现两次的输入顺序吗?你不能在while中使用==或!=,使用!input.equals(EXIT)logic@RAZ_Muh_Taz很好的捕获,错过了。每当我连续尝试向Cart添加项目时,我都会在userInput=keyboard.nextDouble()之后抛出一个keyboard.nextLine();in:这解决了菜单问题。现在我似乎不明白为什么当我显示所有值时,第一个值总是0.00。非常感谢。如果它确实解决了您的问题,请随意标记为“答案”,这样就结束了问题
else if(input.equals(ADD_ITEM)){
     System.out.println("Please enter the price of the item: ");
     userInput = keyboard.nextDouble();
     register.addItem(userInput);    
}