Feb 12, 2012

[Java] for, while, do~while

// Java
// for, while, do~while


import java.util.*;

class for_while_do_while
{
 public static void main(String[] args)
 {
 
        System.out.println("Input number between 1 and 5");
        Scanner scan = new Scanner (System.in);
        int input;
        
        while(!scan.hasNextInt())
        {
            scan.next();  // if input is not integer value, ignore it.
            System.err.println("Input should be integer value");
        }
        
        input=scan.nextInt();  
        
        
        while(input<5)
        {
            System.out.println(input + " + 1 = " + (input+1));
            input=input+1;
        }
                

        do {
            System.out.println("current input value = " + input);
            input=input+1;
        } while(input<7);
            
 
        // for = do + while
        for (input=0; input<5; input=input+2)
        {
            System.out.println("current value in For loop = " + input);
        }
 
 }
}

No comments:

Post a Comment