递归求三角数字
Posted ssdut_yrp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归求三角数字相关的知识,希望对你有一定的参考价值。
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);
以上是关于递归求三角数字的主要内容,如果未能解决你的问题,请参考以下文章