Topic05(控制流程)

2019-02-09  本文已影响0人  KaveeDJ

05.01 if

package pack;

import java.util.Scanner;

public class IsLeapYear {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入年份");
        int year = s.nextInt();
        if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) {
            System.out.println(year + "是闰年");
        } else {
            System.out.println(year + "不是闰年");
        }
    }
}

05.02 switch

05.03 while

package pack;

import java.util.Scanner;

public class factorial {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int n = sc.nextInt();
        int fac = 1;
        while (n >= 1) {
            fac *= n;
            n--;
        }
        System.out.println("阶乘是:" + fac);
    }

}

05.04 for

05.05 continue

package pack;
public class PrintSpecialNumber {

    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if (0 == i%3 || 0 == i%5){
                continue;
            }
            System.out.println(i);
        }
    }
}

05.06 break

package pack;

public class CompoundInterest {
    public static void main(String[] args) {
        int fundPerMonth = 1000;
        int fundPerYear = fundPerMonth * 12;
        float rate = 0.20f;
        
        int sum = 0;
        int target = 1000*1000;
        for (int j = 1; j < 100; j++) {
            int year = j;
            float compoundInterestRate = 1;
            for (int i = 0; i < year; i++) {
                compoundInterestRate = compoundInterestRate * (1 + rate);
            }
            int compoundInterest = (int)(fundPerYear * compoundInterestRate);
            
            sum += compoundInterest;
            System.out.println("经过" + year + " 年,总收入 " + sum);
            
            if(sum >= target) {
                System.out.println("一共需要" + year + "年,累计收入超过" + target);
                break;
            }
        }
    }
}

05.07 结束外部循环

package pack;

public class GoldenCut {

    public static void main(String[] args) {
        // 寻找某两个数相除,其结果离黄金分割点0.618最近
        // 分母和分子不能同时为偶数
        // 分子和分母的取值范围[1,20]
        int range = 20;
        float breakPoint = 0.618f;
        
        float minDiff = 100;
        int answerFenzi = 0;
        int answerFenmu = 0;
        for (int fenzi = 1; fenzi <= range; fenzi++) {
            for (int fenmu = 1; fenmu <= range; fenmu++) {
                // 分母和分子不能同时为偶数
                if (0 == fenzi % 2 & 0 == fenmu % 2) {
                    continue;
                }
                // 取值
                float value = (float) fenzi / fenmu;
                // 取离黄金分割点的差值
                float diff = value - breakPoint;
                // 绝对值
                diff = diff < 0 ? 0 - diff : diff;
                
                // 找出最小的差值
                if (diff < minDiff) {
                    minDiff = diff;
                    answerFenzi = fenzi;
                    answerFenmu = fenmu;
                }
            }
            System.out.println("离黄金分割点(" + breakPoint + ")最近的两个数相除是:" + answerFenzi + "/" + answerFenmu + "=" + ((float)answerFenzi / answerFenmu));
        }
    }

}
package pack;

public class NarcissisticNumber {

    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            int baiwei = i / 100;
            int shiwei = i /10 % 10;
            int gewei = i % 10;
            int cube = baiwei * baiwei * baiwei + shiwei * shiwei * shiwei + gewei * gewei * gewei;
            if (cube == i) {
                System.out.println("找到水仙花数:" + i);
            }
        }
    }
}
package pack;
public class Puzzle {

    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        
        for (a = -100; a <= 100; a++) {
            for (b = -100; b <= 100; b++) {
                for (c = -100; c <= 100; c++) {
                    for (d = -100; d <= 100; d++) {
                        if (a+b==8 && c-d==6 && a+c == 14 && b+d == 10) {
                            System.out.println("a:"+a);
                            System.out.println("b:"+b);
                            System.out.println("c:"+c);
                            System.out.println("d:"+d);
                        }
                    }
                }
            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读