haixi0118 2021-02-10 16:19 采纳率: 80%
浏览 121
已采纳

求问这道题该如何解答呢

This program is a take on the classic programming assignment of creating a grocery list. Your task is to take the store, the food or drink item, and the amount for each item as input and create a grocery list string that can be displayed to the user. For example, the parts in bold were entered by the user.

Once read, the user input will be modified to fit the following rules:

  1. Store name: no leading or trailing whitespace; all characters should be upper case; the only String that may contain a space character (can be multiple words).
  2. All food and drink: no leading or trailing whitespace; all characters should be lower case.

Note to remove any leading/trailing whitespace you may want to consider using the .trim() method for strings!

Below is an example of input, in bold, for the grocery list program. Each amount/weight in the input are followed by their corresponding food/drink (amount1 corresponds to food1, amount3 corresponds to drink, etc).

Store: the Farmer's Market
amount1: 5
food1: Apples
amount2: 12
food2: EggS
amount3: 3
drink: milK
weight1: 1.5
food3: CHEESE
weight2: 3
food4: potatoes

Note the store/amount/food/drink labels will not be included in the input. Thus the input alone will be seen as follows:

the Farmer's Market
5
Apples
12 
EggS  
3
milK
1.5
CHEESE 
3
potatoes

The resulting grocery list is:

Grocery List for Bucky Badger
I am going to THE FARMER'S MARKET to get:

5 apples
12 eggs
3 gallons of milk
1.5 pounds of cheese
3.0 pounds of potatoes

Note: there should always be 5 items in the grocery list, with the third item in units of gallons, and the fourth and fifth items in units of pounds.

Requirements:

  • Your main method should contain all input code (e.g., Scanner instance) and have 11 user prompts inputting values with nextInt(), nextDouble(), next(), and nextLine().
  • All output code (e.g., print statements) must be in the main method as well. The makeGroceryList method takes the arguments passed in and prepares the entire grocery list, using all the 11 parameters and returning one long String. You will need to use string concatenation to create this one long string combining all the variables.
  • You do not need to use the above grocery list. You can try out your own input values.
  • Your grocery list must contain your name as you entered it in the file header comments. It should be a string literal and not passed in as a parameter. For example, at the start of your grocery list "Grocery List for Bucky Badger…" where Bucky Badger is your name.
  • The method stub for makeGroceryList is:
    /**
     * Returns a single String that is formatted as a grocery list that contains all 
     * 11 parameters. You must include your name in the list as a string literal 
     * (don't pass in your name as a parameter). Your name must match the name within 
     * the File Header Comment.
     * 
     * There are no input or output statements in this method. All input should already be done in
     * main (when the user is prompted), and all output should be done in main after this method 
     * returns.
     * 
     * DO NOT MODIFY THE ORDER OF PARAMETERS IN THE METHOD HEADER. If you do, 
     * you may not pass the automatic tests that we use to grade your submission.
     * 
     * @param store
     * @param amount1
     * @param amount2
     * @param amount3
     * @param weight1
     * @param weight2
     * @param food1
     * @param food2
     * @param food3
     * @param food4
     * @param drink
     * @return A grocery list that contains all 11 parameters and your name.
     */
    public static String makeGroceryList(String store, int amount1, int amount2, 
            int amount3, double weight1, double weight2, String food1, String food2, 
            String food3, String food4, String drink) {
        //FILL IN BODY
    }

Note that you may change the makeGroceryList parameter names to be more meaningful, but be sure to keep the order and data types the same. It is good practice to test different combinations of input with your method, but note that we will test your method using specific input for this lab. When calling makeGroceryList, the arguments passed are by position (the order in which they show up as parameters in the method header), so the parameter names do not matter outside the method. Also note that the grocery list should NOT end with a newline character.

Note:

(1) A common error found when running this program is an InputMismatchException error. This occurs when a scanner is looking for one type of object but finds another (for example scnr.nextInt() is looking for an integer when the next value may be a string). Be sure the order in which you read in inputs matches the expected type. Hint: Read 3 L | Tutorial: Using Scanner to make sure you understand the differences among nextInt(), nextDouble(), next(), and nextLine() on reading the input. (Especially how nextLine() is different from others.)

  • 写回答

1条回答 默认 最新

  • ProfSnail 2021-02-11 03:27
    关注
     import java.util.Scanner;
    
     public class GroceryList {
     /**
         * Returns a single String that is formatted as a grocery list that contains all 
         * 11 parameters. You must include your name in the list as a string literal 
         * (don't pass in your name as a parameter). Your name must match the name within 
         * the File Header Comment.
         * 
         * There are no input or output statements in this method. All input should already be done in
         * main (when the user is prompted), and all output should be done in main after this method 
         * returns.
         * 
         * DO NOT MODIFY THE ORDER OF PARAMETERS IN THE METHOD HEADER. If you do, 
         * you may not pass the automatic tests that we use to grade your submission.
         * 
         * @param store
         * @param amount1
         * @param amount2
         * @param amount3
         * @param weight1
         * @param weight2
         * @param food1
         * @param food2
         * @param food3
         * @param food4
         * @param drink
         * @return A grocery list that contains all 11 parameters and your name.
         */
        public static String makeGroceryList(String store, int amount1, int amount2, 
                int amount3, double weight1, double weight2, String food1, String food2, 
                String food3, String food4, String drink) {
            String str = "Grocery List for haixi0118\n";
            str += "I am going to " + store.trim().toUpperCase() +" to get:\n";
            str += amount1 + " " + food1.trim().toLowerCase() + "\n"; 
            str += amount2 + " " + food2.trim().toLowerCase() + "\n"; 
            str += amount3 + " gallons of " + drink.trim().toLowerCase() + "\n"; 
            str += weight1 + " pounds of " + food3.trim().toLowerCase() + "\n"; 
            str += weight2 + " pounds of " + food4.trim().toLowerCase() + "\n";
            return str;
            //FILL IN BODY
        }
        public static void main(String[] args){
            Scanner scn = new Scanner(System.in);
            String store = scn.nextLine();
            int amount1 = scn.nextInt();
            scn.nextLine();
            String food1 = scn.nextLine();
            int amount2 = scn.nextInt();
            scn.nextLine();
            String food2 = scn.nextLine();
            int amount3 = scn.nextInt();
            scn.nextLine();
            String drink = scn.nextLine();
            double weight1 = scn.nextDouble();
            scn.nextLine();
            String food3 = scn.nextLine();
            double weight2 = scn.nextDouble();
            scn.nextLine();
            String food4 = scn.nextLine();
            String ans = makeGroceryList(store, amount1, amount2, amount3, weight1, weight2, food1, food2, food3, food4, drink);
            System.out.println(ans);
        }
     }
    

     

     

    记得在//我的名字那一栏,把名字改成自己的,不然就记不上分了。

     

    经测试,输出的内容为:

     

    Grocery List for haixi0118
    I am going to THE FARMER'S MARKET to get:
    5 apples
    12 eggs
    3 gallons of milk
    1.5 pounds of cheese
    3.0 pounds of potatoes

     

    测试无误,可用。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上