美文网首页
public class Demo 练习

public class Demo 练习

作者: 哈迪斯Java | 来源:发表于2023-07-22 15:14 被阅读0次

    public class Demo {
    public static void main(String args[]) {
    double sum = 0, a = 1;
    int i = 1;
    while (i <= 20) {
    sum = sum + a;
    i = i + 1;
    a = a * (1.0 / i);
    }
    System.out.println(sum);
    }
    }

    public class SingleNum {
    public static void main(String[] args) {
    int count = 0;
    for (int i = 1; i < 101; i++) {
    // 默认是素数
    boolean flag = true;
    for (int j = 2; j <= Math.sqrt(i); j++) {
    if (i % j == 0) {
    // 能整除
    flag = false;
    }
    }
    if (flag) {
    count += 1;
    System.out.print(i + ",");
    }
    }
    System.out.println("\n共计" + count + "个素数");
    }
    }

    相关文章

      网友评论

          本文标题:public class Demo 练习

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