Pizza添加Toppings部分填充数组java

时间:2016-02-02 02:30:19

标签: java

enter image description here

我不知道在我的阵列中添加一个新的顶级名称,最后我必须添加元素但是我不知道在哪里使用循环到目前为止我在我的代码中有这个。谢谢 - 这个项目背后的想法是你将使用一个部分填充的数组来维护一系列的披萨配料。将有一个固定的可用配料列表供用户选择。用户将能够添加/删除浇头列表中的浇头,这是输出:

Currently no toppings selected
Enter Option: (A-Add Topping; R-Remove Topping; X-Exit): a
Possible toppings are:
0 Sausage
1 Pepper
2 Pepperoni
3 Brocolli  didn’t realize I misspelled it until pasting into Word…sigh
4 Tomatoes
5 Eggplant
6 Meatballs
7 Ham
8 Pineapple
9 Bacon
Enter # of topping to add: 6
Toppings Selected:
0 Meatballs
Enter Option: (A-Add Topping; R-Remove Topping; X-Exit): a
Possible toppings are:
0 Sausage
1 Pepper
2 Pepperoni
3 Brocolli
4 Tomatoes
5 Eggplant
6 Meatballs
7 Ham
8 Pineapple
9 Bacon
Enter # of topping to add: 0
Toppings Selected:
0 Meatballs
1 Sausage
Enter Option: (A-Add Topping; R-Remove Topping; X-Exit): a
Possible toppings are:
0 Sausage

我的代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pizzatopfinal_v01;

import java.util.Scanner;

/**
 *
 * 
 */
public class PizzaTopFinal_v01 {

    /**
     * @param args the command line arguments
     */


    public static void main(String[] args) {
        // TODO code application logic here
        showDisplay();




       // }while(input.equalsIgnoreCase("A")||input.equalsIgnoreCase("R"));

    }   //end of main method 


    public static void addTopping(int numTopp){
    String[] topping = { "Sausage"," Pepper"," Pepperoni",
            "Brocolli", "Tomatoes"," Eggplant",
            "Meatballs"," Ham"," Pineapple"," Bacon"};

        int topp= numTopp;
        System.out.println("Topping selected:");                

        String name; 
        name = topping[topp]; 
        final int length  = 100;
        String[] newToppingArray = new String[length];
        int size=0;

        newToppingArray[size]=name;
        System.out.println(size + "\t" + newToppingArray[size]);
        size++;
        showDisplay();

        //for(int size=0;size<newToppingArray.length;size++)
        //System.out.println(topping[topp]);

    } // end of method addTopping 

    public static void showDisplay(){

    String input;

        System.out.println("Currently no toppings selected");
        System.out.print("Enter option: (A-Add Topping; R-Remove Topping "
                + "; X-Exit):");
        Scanner keyboard = new Scanner(System.in);
        input = keyboard.nextLine();
        System.out.println("Possible toppings are:");
        System.out.println("0 Sausage");
        System.out.println("1 Pepper");
        System.out.println("2 Pepperoni");
        System.out.println("3 Brocolli");
        System.out.println("4 Tomatoes");
        System.out.println("5 Eggplant");
        System.out.println("7 Ham");
        System.out.println("8 Pineapple");
        System.out.println("9 Bacon"); 

     if(input.equalsIgnoreCase("A")){
            //System.out.println("you selected add topping");
            System.out.print("Enter # of topping to add :");

            //create a Scanner object to hold the number option 
            int numOfTopping;
            numOfTopping = keyboard.nextInt();
            addTopping(numOfTopping);
            //System.out.println("congrats you enter: "+ numOfTopping);

                                        }
       else if(input.equalsIgnoreCase("R"))
            System.out.println("You selected Remove topping");
       else if(input.equalsIgnoreCase("X"))
            System.out.println("You selected Exit Program bye bye ");
       else
           System.out.println("Not valid option! ");
    }



}   //end of the class 

0 个答案:

没有答案