自学内容网 自学内容网

Java实验 三

2, 判断100以内的素数

package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        int j;
        for (int i = 2; i < 100; i++) {
            for (j = 2; j < i; j++) {
                if (i % j == 0) {
                    break;
                }
            }
            if (j >= i / 2 + 1) {
                System.out.print(i + " ");
            }
        }
    }
}
package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        int i, j;
        boolean flag;
        for (i = 2; i <= 100; i++) {
            flag = true;
            for (j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                }
            }
            if (flag) {
                System.out.print(i + " ");
            }
        }
    }
}

3.水仙花数

package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        int a, b, c;
        for (int  i = 100; i <= 999; i++) {
            a = i / 100;
            b = (i / 10) % 10;
            c = i % 10;
            if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i) {
                System.out.println(i + " ");
            }
        }
    }
}

4.编写程序计算 1-1/2+1/3-1/4.....前10项,小数点后面撆留2位,将下列程序补充完整并运潎,撆存为:e306.java

package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        int i = 1;
        double sum = 0, t = 1.0, f = 1;
        while (i <= 10) {
            sum += t;
            i++;
            f = -f;
            t = f * (1.0 / i);
            System.out.println(t);
        }
        System.out.printf("%.2f", sum);
    }
}

5.编写撊序在键撉中输撋一个整撀,撍撎其撌否素撀。

package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        boolean f = true;
        for (int i = 3; i < Math.sqrt(n) + 1; i++) {
            if (n % i == 0) {
                f = false;
                System.out.println("false");
                break;
            }
        }
        if (f) {
            System.out.println("true");
        }
    }
}

6.

在键撉撂入一系列双精度数,潵撖撂入0时,停止撂入。求这一撐列数据之和并撂撔,要求小数撏后面保留2位

package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        double n;
        Scanner input = new Scanner(System.in);
        double sum = 0.0;
        do {
            n = input.nextDouble();
            sum += n;
        } while (n != 0);
        System.out.println(sum);
    }
}

7.冒泡排序

package com.my.gududu;

import java.util.*;

public class dadada{
    public static void main(String[] args) {
        int arr[] = new int[10];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            arr[i] = input.nextInt();
        }
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 8 - i; j++) {
                if (arr[j] >= arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
        for (int i = 0; i < 10; i++) {
            System.out.printf("%d ", arr[i]);
        }
    }
}


原文地址:https://blog.csdn.net/2401_82661391/article/details/142489351

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!