栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA编程思想第四章练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA编程思想第四章练习题

​​​​​​​练习1: 

package JAVA4;

public class E01Print {
    public static void main(String[] args) {
        for(int i=0;i<101;i++)
            System.out.println(i);
    }
}

练习2:

package JAVA4;

import java.util.Random;

public class E02 {
    static Random r = new Random(47);
    public static void compareRand(){
        int a = r.nextInt();
        int b = r.nextInt();
        System.out.println("a = " + a + " b = " + b);
        if(ab)
            System.out.println("a > b");
        else System.out.println("a = b");
    }

    public static void main(String[] args) {
        for(int i =0;i<=25;i++)
            compareRand();
    }
}

练习3:

package JAVA4;

public class E03 {
    public static void main(String[] args) {
        while (true)
            E02.compareRand();
    }
}

练习4:

package JAVA4;

public class E04 {
    public static void sushu(){
        for(int i=1;i<=100;i++) {
            boolean prime = true;
            for (int j = 2; j < i; j++)
                if (i % j == 0)
                    prime = false;
            if(prime)
                System.out.print(i+" ");
        }
    }

    public static void main(String[] args) {
        sushu();
    }
}

练习5:

​​​​​​​

package JAVA4;

public class E06 {
    static boolean test(int testval, int begin, int end){
        boolean result = false;
        if(testval >= begin && testval <= end)
            result = true;
        return  result;
    }

    public static void main(String[] args) {
        System.out.println(test(10,5,15));
        System.out.println(test(5,10,15));
        System.out.println(test(5,5,15));
    }
}

package JAVA4;

public class E07 {
    static int print() {
        int i =0;
        for (i=0; i < 101; i++) {
            System.out.println(i);
            if (i >= 99)
                break;
        }
        return ++i;
    }

    public static void main(String[] args) {
        System.out.println(print());
    }
}

练习8:

package JAVA4;

public class E08 {
    public static void main(String[] args) {
        for(int i =0;i<5;i++)
            switch (i){
                case 1:
                    System.out.println("1");
                    break;
                case 2:
                    System.out.println("2");
                    break;
                case 3:
                    System.out.println("3");
                    break;
        }
    }
}

练习9:

package JAVA4;

import java.lang.reflect.Field;
import java.util.Scanner;

public class E09 {

    public static int Fibonacci(int a) {
        if (a <= 2)
            return 1;
        return Fibonacci(a - 1) + Fibonacci(a - 2);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        for(int i=1;i<=a;i++)
            System.out.println(Fibonacci(i)+" ");
    }
}

练习10:(不会)

package JAVA4;

public class E10 {
    public static void main(String[] args) {
        int[] starDigit = new int[4];
        int[] productDigit = new int[4];
        for (int num1 = 10; num1 <= 99; num1++)
            for(int num2 = num1;num2 <= 99; num2++){
                if((num1 * num2) % 9 != (num1 + num2) % 9)
                    continue;
                int product = num1 * num2;
                starDigit[0] = num1 / 10;
                starDigit[1] = num1 % 10;
                starDigit[2] = num2 / 10;
                starDigit[3] = num2 % 10;
                productDigit[0] = product /1000;
                productDigit[1] = (product % 1000) / 100;
                productDigit[2] = product % 1000 % 100 / 10;
                productDigit[3] = product % 1000 % 100 % 10;
                int count =0;
                for(int x = 0; x < 4; x++ )
                    for(int y= 0;y < 4; y++)
                        if(productDigit[x] == starDigit[y]){
                            count++;
                            productDigit[x] = -1;
                            starDigit[y] = -2;
                            if (count == 4)
                                System.out.println(num1 + " * " + num2 + " : " + product);
                        }
            }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785799.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号