码迷,mamicode.com
首页 > 其他好文 > 详细

问题1-5

时间:2014-09-22 13:48:12      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   java   ar   for   问题   on   

1,生兔子问题(斐波那契数列)

public class Fibonacci {
public static void main(String[] args) {
    System.out.println("The rabit of 1th month : 1");
    System.out.println("The rabit of 2th month : 2");
    int f1=1,f2=1,f,m=24;
    for(int i=3;i<=m;i++){
        f=f2;
        f2=f1+f2;
        f1=f;
        System.out.println("The "+i+"th"+" month‘s rabit is "+f2);
    }
}
}

2,求100至201之间的素数:

public class primeNum {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 101; i < 200; i += 2) {
            boolean b = false;
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    b = false;
                    break;
                } else {
                    b = true;
                }
            }
            if (b == true) {
                count++;
                System.out.println("The prime number is " + i);
            }
        }
        System.out.println("The Number of prime number is " + count);
    }
}

 

3,水仙花数(自恋数):

public class narcissisticNum {
public static void main(String[] args) {
    int m,n,l;
    for(int k=101;k<1000;k++){
        m=k/100;
        n=k%100/10;
        l=k%10;
        if((m*m*m+n*n*n+l*l*l)==k){
            System.out.println(k+" is so Narcissitic!!!");
        }
    }
}
}

4,分解质因数:

import java.util.Scanner;

public class DecompositionOfTheQualityFactor {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input a int number: ");
        int n = scanner.nextInt();
        int k = 2;
        System.out.print(n + "=");
        while (k <= n) {
            if (k == n) {
                System.out.print(n);
                break;
            } else if (n % k == 0) {
                System.out.print(k + "*");
                n = n / k;
            } else {
                k++;
            }

        }
    }
}

5,成绩等级判断:

import java.util.Scanner;

public class ScoreGrade {
    public static void main(String[] args) {
        int x;
        char grade;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input a score:");
        x = scanner.nextInt();
        grade = x >= 90 ? ‘A‘ : x >= 60 ? ‘B‘ : ‘C‘;
        System.out.println("The grade is: " + grade);
    }
}

问题1-5

标签:style   color   io   os   java   ar   for   问题   on   

原文地址:http://www.cnblogs.com/vonk/p/3985635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!