メソッド factorial の注釈を印刷することができません。階乗に値を返させず、結果をメソッド自体に出力しないと、機能します。私はここで問題を理解していません。
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface Store
{
int id();
String developerName();
String createdDate();
String Copyrightmessage();
}
public class Ch10LU1Ex2
{
@Store(id = 1, developerName = "Robin", createdDate = "03/Jan/2013", Copyrightmessage = "Cannot copy anything")
public static int factorial(int n)
{
int result;
if(n==1)
return 1;
result = factorial(n-1) * n ;
return result;
}
public static void main(String[] args)
{
try
{
Ch10LU1Ex2 ch = new Ch10LU1Ex2();
System.out.println("Enter any number from 0 to 10 to find factorial:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int ch1 = Integer.parseInt(br.readLine());
int x = ch.factorial(ch1);
System.out.println("The factorial is:"+x);
Method method = ch.getClass().getMethod("factorial");
Annotation[] annos = method.getAnnotations();
for(int i=0; i<annos.length;i++)
{
System.out.println(annos[i]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}