美文网首页
Java基础程序

Java基础程序

作者: Girl_endless | 来源:发表于2016-04-11 19:12 被阅读15次

    生兔子问题:

    package com.atguigu.exer;
    
    import java.util.Scanner;
    
    public class Fibonacci {
    
        public static void main(String[] args) {
             Scanner scanner = new Scanner(System.in);
             System.out.println("Please input n:");
             int n = scanner.nextInt();
             
             System.out.println("当前值为" + fibonacci(n));
             
             int sum = 0;
             for(int i = 1; i <= n; i++){
                 sum += fibonacci(n);
             }
             System.out.println(sum);
        }
    
    
        public static int fibonacci(int n){
            if(n < 2){
                return 1;
            }else {
                return fibonacci(n-1) +fibonacci(n-2);
            }
            
        }
    }
    

    统计字符个数:

    package com.atguigu.exer;
    
    public class total {
        public static void main(String[] args){
            String s = "aasddsfdg213SS";
            
            int countNum = 0;
            int countUpper = 0;
            int countLower = 0;
            int countOther = 0;
            
            for(int i = 0; i < s.length(); i ++){
                char c = s.charAt(i);
                
                if (c >= 'A' && c <= 'Z') {
                    countUpper++;
                }else if (c >= 'a' && c <= 'z') {
                    countLower++;
                }else if (c >= '0' && c <= '9') {
                    countNum++;
                }else {
                    countOther++;
                }
            }
            System.out.println(countLower);
            System.out.println(countUpper);
            System.out.println(countNum);
            System.out.println(countOther);
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:Java基础程序

          本文链接:https://www.haomeiwen.com/subject/dcpilttx.html