I just want to learn. I just want to learn. - 20 days ago 7
Java Question

How to make loop run as many times as user inputs?

I have a for loop and the method to call it is printMe() and I want the user to be able to determine how many times it runs with their input. This is along the lines of what I think it should be:

System.out.println("Enter a number: ");
loopNum = input.nextInt();

Answer

This should work:

Scanner input = new Scanner(System.in);
int loop = input.nextInt();
for(int i = 0; i<loop;i++){
    //do the thing
}
Comments