Assingment #78: Counting with a For Loop

Code

/// Name: Kiran O'Farrell
/// File name: CountingFor.java
/// Program name: Counting with a For Loop
/// Date completed: 2/22/16

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 = 1 ; n <= 5 ; n = n+1 ) {
        
            System.out.println( n + ". " + message );
        }
        
    }
    
}
    
n = n+1 tells the program how many times it has completed the loop. int n = 1 tells the program where to start. To make the code repeat 10 times, change "n <= 5" to "n <= 10". To make the code start at 2 and count by 2's, replace "int n = 1" with "int n = 2", and replace "n = n+1" with "n = n+2".

Picture of the output