Assignemnt #78 and Counting with a For Loop
Code
import java.util.Scanner;
public class CountingFor
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println( "Type in a message, and I'll display it five times." );
System.out.print( "Message: " );
String message = keyboard.nextLine();
for ( int n = 2 ; n <= 11 ; n = n+2 )
{
System.out.println( n + ". " + message );
}
}
}
/*
1. (n = n + 1) makes it so that every time the loop is run, n will be one digit higher. Without it, the loop will run forever because n is not changing.
2. int n = 1, gives n a value for the first loop. So for instance "int n = 1" initializes n as 1 when the loop starts.
*/
Picture of the output