java扫描仪问题我一直在大学做作业,但我在扫描仪捕获数据方面遇到了困难

问题描述 投票:0回答:1

我遇到的问题是,我创建了一系列字符串,其中包含带有项目名称的数组和用于价格的双精度数组。适合三组条件:花朵、颜色、尺寸。我还为菜单创建了一个切换方法,现在这就是我陷入困境的地方。当我调用扫描仪获取有关花朵、颜色和尺寸的信息时,它不起作用。菜单显示了,但如果我去订购列出的鲜花,它们就不会显示。有人建议我不要使用扫描仪,还有其他方法可以做到这一点吗?如果可以的话,该怎么做?

import java.util.Scanner;
public class MenuTest2B {

        // create flowers + price arrays

    public static final String[] FLOWERS = {"Roses", "Lilys", "Carnations", "Daffodils", "Gerberas", "Chrysanthemums", "Assorted"};
    public static final double[] FLOWER_PRICES = {1.2, 1.3, 1.0, 1.0, 1.1, 1.1, 0.8};
        // create colours + price arrays
    public static final String[] COLOURS = {"White", "Red", "Pink", "Yellow", "Blue", "Mixed"};
    public static final double[] COLOUR_PRICES = {1.3, 1.2, 1.1, 1.1, 1.2, 1.0};
        // create size + price arrays
    public static final String[] SIZES = {"Small", "Medium", "Large"};
    public static final double[] SIZE_PRICES = {5.5, 7.5, 9.5};

    public static void main(String[] args) {
      

        try (Scanner scanner = new Scanner(System.in)) {
            int Choice;
            
            do
            {
                displayMenu();

                
                
                Choice = getUserChoice(scanner);
                
                switch (Choice) {
                    case 1:
                        // use scanner to display menu options for flowers col ours and size 
                        orderDetailsAndPriceCalculation(scanner);

                        break;
                    case 2:

                        summaryStatistics();
                        break;

                    case 3:
                        System.out.println("You are exiting the program... Goodbye");
                        break;

                    default:
                        System.out.println("Invalid input. Please select a valid option.");
                }
            } while (Choice != 3);
        }
    }

    private static void displayMenu() {
        System.out.println("Main Menu");
        System.out.println("1. Order a bouquet and get the price.");
        System.out.println("2. Display statistics");
        System.out.println("3. Exit");
    }

    public static int getUserChoice(Scanner scanner) {
        int choice = -1;
        try {
            choice = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            // Invalid input, choice will remain -1
        }
        return choice;
    }

    public static void orderDetailsAndPriceCalculation(Scanner scanner) {
        int i;

         // adds values for chosen ammounts for orders and puts them into the scanner
     int flowerChoice = getFlowerChoice(scanner);
     if (flowerChoice == -1) return;

     int colourChoice = getColourChoice(scanner);
     if (colourChoice == -1) return;

     int sizeChoice = getSizeChoice(scanner);
     if (sizeChoice == -1) return;
    
         // calculates the input order details (Flower+Colour)*Size 
     double totalPrice = (FLOWER_PRICES[flowerChoice] + COLOUR_PRICES[colourChoice]) * SIZE_PRICES[sizeChoice];

         // PRINT TOTAL AMOUNT
         // AND DETAILS OF BOUQUET

     System.out.println("Bouquet: " + SIZES[sizeChoice] + " " + COLOURS[colourChoice] + " " + FLOWERS[flowerChoice] + ".");
     System.out.println("Bouquet Price: £" + totalPrice); 
    }

    public static void summaryStatistics() {
        System.out.println("Summary statistics provided");
    }
}
java
1个回答
0
投票

我建议对您的代码进行一些更改,首先在main中, 在其中,我们创建一个 MenuTest2B 对象,并用它来调用 init() 这将执行程序,这样,我们就避免了所有那些丑陋的static(和不必要的)。

我还修改了属性FLOWERSFLOWER_PRICESCOLOURS,在其中添加短语“选择你的”及其自己的描述,这将帮助我创建相应的菜单。

创建 menu 属性,目的相同。

为了理解这些修改,可以使用 displayMenu() 方法,该方法接收相应的数组并打印菜单。

我删除了 Scanner 声明并将其作为属性,以便正确关闭它,并添加了 getInput() 方法,该方法返回已处理的用户输入。

exec 中,将 displayMenu(); 更改为 displayMenu(menu );,因为它现在需要参数。

orderDetailsAndPriceCalculation() 中添加对 displayMenu( FLOWERS )displayMenu( COLORS )displayMenu( SIZES ) 的调用,这些调用是缺失的。

仍然需要实施summaryStatistics,我把它留给你了。

当然,您会注意到,在价格数组中,我添加了第一个值“0”,这是为了使所选值与用户的选择相匹配(在FLOWERSFLOWER_PRICESCOLOURS中添加,具有相同的效果)。

public class MenuTest2B {

   public final String[] FLOWERS = { "Choose your flower:", "Roses", "Lilys", "Carnations", "Daffodils", "Gerberas", "Chrysanthemums", "Assorted" };
   public final double[] FLOWER_PRICES = { 0, 1.2, 1.3, 1.0, 1.0, 1.1, 1.1, 0.8 };
   // create colours + price arrays
   public final String[] COLOURS = { "Choose your colour:", "White", "Red", "Pink", "Yellow", "Blue", "Mixed" };
   public final double[] COLOUR_PRICES = { 0, 1.3, 1.2, 1.1, 1.1, 1.2, 1.0 };
   // create size + price arrays
   public final String[] SIZES = { "Choose your size:", "Small", "Medium", "Large" };
   public final double[] SIZE_PRICES = { 0, 5.5, 7.5, 9.5 };
   private Scanner scanner;
   public final String[] menu = { "Main Menu", "1. Order a bouquet and get the price.", "2. Display statistics", "3. Exit" };

   private void displayMenu( String obj[] ) {
      for( int i = 0; i < obj.length; i ++ ) {
         if( i == 0 ) {
            System.out.println( obj[ i ] );
         }
         else {
            System.out.println( i + ". " + obj[ i ] );
         }
      }
   }

   public void init() {
      scanner = new Scanner( System.in );
      int choice;
      do {
         displayMenu( menu );
         choice = getInput();
         switch( choice ) {
            case 1:
               // use scanner to display menu options for flowers col ours and size 
               orderDetailsAndPriceCalculation();
               break;
            case 2:
               summaryStatistics();
               break;
            case 3:
               System.out.println( "You are exiting the program... Goodbye" );
               scanner.close();
               break;
            default:
               System.out.println( "Invalid input. Please select a valid option." );
         }
      }
      while( choice != 3 );

   }

   public void orderDetailsAndPriceCalculation() {
      int i;
      // adds values for chosen ammounts for orders and puts them into the scanner
      System.out.println( "" );
      displayMenu( FLOWERS );
      int flowerChoice = getInput();
      if( flowerChoice == -1 ) {
         return;
      }
      displayMenu( COLOURS );
      int colourChoice = getInput();
      if( colourChoice == -1 ) {
         return;
      }
      displayMenu( SIZES );
      int sizeChoice = getInput();
      if( sizeChoice == -1 ) {
         return;
      }
      // calculates the input order details (Flower+Colour)*Size 
      double totalPrice = ( FLOWER_PRICES[ flowerChoice ]
              + COLOUR_PRICES[ colourChoice ] )
              * SIZE_PRICES[ sizeChoice ];    
      // PRINT TOTAL AMOUNT
      // AND DETAILS OF BOUQUET
      System.out.println( "Bouquet: " + SIZES[ sizeChoice ] + " " + COLOURS[ colourChoice ] + " " + FLOWERS[ flowerChoice ] + "." );
      System.out.println( "Bouquet Price: £" + totalPrice );
   }

   public void summaryStatistics() {
      System.out.println( "Summary statistics provided" );
   }

   int getInput() {
      try {
         return scanner.nextInt();
      }
      catch( Exception e ) {
         return -1;
      }
   }

   public static void main( String[] args ) {
      MenuTest2B mt2 = new MenuTest2B();
      mt2.exec();
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.