zoukankan      html  css  js  c++  java
  • 递归-第N项是由第N-1项加n得到的

    package lhh.dataStructureAndAlgorithm;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     * @program: IdeaJava
     * @Date: 2019/11/17 16:44
     * @Author: lhh
     * @Description: 第N项是由第N-1项加n得到的,
     */
    public class TrangleApp {
    
        static int theNumber;
    
        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);
        }
    
    
        public static int trangle(int n) {
            if(n == 1) return 1;
            else return (n+trangle(n-1));
        }
    
        public static void main(String[] args) throws IOException {
            System.out.print("Enter a number: ");
            theNumber = getInt();
            int theAnswer = trangle(theNumber);
            System.out.println("Triangle="+theAnswer);
        }
    }
  • 相关阅读:
    python 文件目录/方法
    python文件
    python模块
    python数据结构
    python函数
    python迭代器和生成器
    python循环语句
    python控制语句 if
    python数字
    个人课程总结
  • 原文地址:https://www.cnblogs.com/lhh666/p/11877428.html
Copyright © 2011-2022 走看看