How do i write a for loop that will repeat the number of times the user wants it too when asked by the console?
for loop that will repeat the number of times the user wants.
#4 Members - Reputation: 347
Posted 12 January 2013 - 02:23 AM
The code for the for loop will be the same but here:
//imported crap and other code
Scanner scan = new Scanner(System.in);
int numLoops;
System.out.print("How many loops?");
numLoops = scan.nextInt();
for(int i = 0; i < numLoops; ++i){
//do whatever you need to and loop as many times as the user entered for numLoops
}
you can do the same thing with a while loop (or do while), but the for loop has everything working in one line, where the while loops will take a couple.
Hope this helps ![]()
Edited by Mathew Bergen, 12 January 2013 - 02:24 AM.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#5 Members - Reputation: 314
Posted 12 January 2013 - 12:46 PM
where the while loops will take a couple
Remember C/C++ and as far as I know Java too, don't use line endings a seperators.
You can happily write your whole application in one line, as long as the compiler supports it and you don't miss any semicolons.
#6 Members - Reputation: 347
Posted 12 January 2013 - 02:44 PM
where the while loops will take a couple
Remember C/C++ and as far as I know Java too, don't use line endings a seperators.
You can happily write your whole application in one line, as long as the compiler supports it and you don't miss any semicolons.
I understand his, however i said this because the only time i put more than one "line" of code on one line, is in simple switch statements,
switch(choice){
case 1: /*goto function*/ break;
...
}
and in the code that other people write, i have never seen more that one "line" of code on a single line. And if you where to write a whole application on one line, it would be very confusing, unless it was a hello world program.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"






