您的位置:首页 > 理论基础 > 数据结构算法

递归 三角数字 Java数据结构与算法

2010-07-18 14:26 337 查看
递归 三角数字 Java数据结构与算法

源代码:

/**
*
* @author sunnyykn
*/
import java.io.*;
class TriangleApp
{
static int theNumber;
public static void main(String[] args) throws IOException
{
System.out.print("Enter a number:");
theNumber = getInt();
int theAnswer = triangle(theNumber);
System.out.println("Triangle = " + theAnswer);
}
public static int triangle(int n)
{
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: