Feb 12, 2012

[Java] method with reflection

// Java
// method with reflection


import java.util.*;

class method_with_reflection
{

// A java program must start with "main" method
 public static void main(String[] args)    // main method
 {
 
        System.out.println("first number for factorial?");
        Scanner scan1 = new Scanner (System.in);
        
        while(!scan1.hasNextDouble())
        {
            scan1.next();  // if input is not integer value, ignore it.
            System.err.println("Input should be number");
        }
        double n1=scan1.nextDouble(); 
        
        System.out.println("second number for factorial?");
        Scanner scan2 = new Scanner (System.in);
        
        while(!scan2.hasNextDouble())
        {
            scan2.next();  // if input is not integer value, ignore it.
            System.err.println("Input should be number");
        }
        double n2=scan2.nextDouble(); 

        if(n1==0 && n2==0) {
            bye();    // if weight was input 0, call method "bye"
            System.exit(0); }
                

        System.out.println();        
        System.out.println(n1 + " factorial = " + factorial(n1));  
        System.out.println(n2 + " factorial = " + factorial(n2)); 

}


 public static double factorial(double n)
 {
        if (n==1)
            return 1;
        else
            return n*factorial(n-1);
 }
 

 public static void bye()
 {
        System.out.println();        
        System.out.println("Good Bye");
 }
}

No comments:

Post a Comment