I'm relatively new to programming. I'm trying to make a program at the moment, and I'm trying to figure out how I can do something. I hope you guys can help me as I just don't know how to do this...

So, first of all, I have made an array and filled it with stuff:

String[] pizza = new String[10];
    pizza[0] = "1- Cheese";
    pizza[1] = "2- Wedge";
    pizza[2] = "3- Bacon";
    pizza[3] = "4- Hawaiian";
    pizza[4] = "5- Vegetarian";
    pizza[5] = "6- Pepperoni";
    pizza[6] = "7- Ham";
    pizza[7] = "8- Apple";
    pizza[8] = "9- Grape";
    pizza[9] = "10- Italian";

I want to make it so that I have an IF statement (which is inside a while). I'll just put the code here, and explain after.

int pizzaCounter = 0;

        while(pizzaCounter < 5)
        {

            Scanner pizzaPick = new Scanner(System.in);
            int Pizzas = pizzaPick.nextInt();

            if (Pizzas == 1)
            {
                *Insert something here*

                pizzaCounter++;
            }
            if (Pizzas == 2)
            {
                *Insert something here*

                pizzaCounter++;
            }
            if (Pizzas == 3) etc...

        }

Now at the 'Insert something here' bit, I want to try to make it so that it stores the text from the array(pizza) into some variable which I can print out later... So for example if the user inputs '1' then it takes: "1-Cheese" and stores it in a variable which I can print out later...

Also, I want to make it clean, so that there aren't 10 IF statements prompting each variable thing...?

I don't know if this is even possible, but any help is greatly appreciated! :D

I hope what I am asking here is understandable...

Please, if possible, could you explain what you are doing at each of the steps, so that I can actually understand what is happening, and maybe use the same code later, instead of just copying it and pasting the code? I'm kind of a noob so I think that the more I learn the more I can use later on... Thanks so much! :D

link
1  
look up switch statement – Thunder Rabbit 1 min ago
feedback

1 Answer

You can replace the entire series of if statements with something like:

string saveName = "";
if ((Pizzas >= 0) && (Pizzas <= 9)) {
    saveName = pizza[Pizzas];
    pizzaCounter++;
}
link
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.