import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; //================================================= // File Name : triangle_demo //------------------------------------------------------------------------------ // Author : Common //类名: //属性: //方法: //主类 //Function : triangle_demo public class triangle_demo { //static int theNumber; public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 System.out.println("输入数字:"); int theNumber = getInt(); int theAnswer = triangle(theNumber); System.out.println("三角上每一行的数量:"+theAnswer); } public static int triangle(int n){ //递归输出1 3 6 10.... if(n==1) return 1; else return (n + triangle(n-1)); } //输出方法 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 getInt() throws IOException{ String s = getString(); return Integer.parseInt(s); } }