역귀구 삼각 숫자

2561 단어 귀속
package cn.gwssi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TriangleApp {
    /** * @param args */
    static int theNumber;   
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        System.out.print("Enter a number:");
        theNumber=getInt();
        int theAnswer=triangle(theNumber);
        System.out.println("Triangle="+theAnswer);
    }

    public static String getString() throws IOException{
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    }

    public static int triangle(int n){
        if(n==1)
            return 1;
        else
            return (n+triangle(n-1));
    }

    public static int getInt() throws IOException{
        String s =getString();
        return Integer.parseInt(s);
    }
}

좋은 웹페이지 즐겨찾기