Assignemnt #107 Nesting Loop
Code
/// Name:JJ Deng
/// Period: 6
/// Program Name: Nesting Loops
/// File Name: NestingLoops.java
/// Date Finished: 4/27/2016
import java.util.Scanner;
import java.util.InputMismatchException;
public class NestingLoops
{
public static void main( String[] args )
{
// this is #1 - I'll call it "CN"
for ( int n=1; n <= 3; n++ )
{
for ( char c='A'; c <= 'E'; c++ )
{
System.out.println( c + " " + n );
}
}
System.out.println("\n");
// 1. Variable n changes faster, because it changes three times when c changes each time. It is inside.
// 2. c changes faster then n, because cn loop is inside.
// 3. It displayed 2 numbers on each line.
// 4. It displayed 6 numbers on each line, because println() only performs 1 time for 3 changes of B (1 change of A).
// this is #2 - I'll call it "AB"
for ( int a=1; a <= 3; a++ )
{
for ( int b=1; b <= 3; b++ )
{
System.out.print( a + "-" + b + " " );
}
System.out.println("");
}
System.out.println("\n");
}
}
Picture of the output