mercredi 7 octobre 2015

Ask for # of variables based on input (for/while statement?)

Hi I'm pretty new to java, and I was wondering if there was a way to ask the user to input a certain number of variables based on an earlier user input. I don't want to make an if statement for every possible user input, because that is messy and tedious.

I am asking the user for a grid size, and then I want to ask them to fill each box with a number.

I was thinking I could use a for or while statement to keep asking the user for a new number until the height and width of the grid was reached, but I don't know how to only ask for one variable at a time, then change the variable name the next time it asks the user (the next time it loops).

I hope this makes sense, please help haha. I kinda want it to work like this:

for(int i=0; i<height; i++){
            System.out.print("Enter next number");
            (variable+1)=scan.nextInt();
        }

And save each variable in an increasing order.

Aucun commentaire:

Enregistrer un commentaire