zoukankan      html  css  js  c++  java
  • Java日志第48天 2020.8.24

    3.11 用do-while求1+2+3+...+100的值

    public class Demo3_11 {
        public static void main(String[] args) {
            int i = 1;
            int sum = 0;
            do{
                sum += i;
                i++;
            } while (i<=100);
            System.out.println("sum="+sum);
        }
    }

     

     

    3.12 用下面公式求π的近似值

     

    直到最后一项的绝对值小于为止。

    import java.text.DecimalFormat;

    public class Demo3_12 {
        public static void main(String[] args) {
            int s =1;
            double n = 1, t=1, pi=0;
            while (Math.abs(t) > 1e-7) {
                pi = pi+t;
                n = n+2;
                s = -s;
                t = s/n;
            }
            pi = pi * 4;
            DecimalFormat df = new DecimalFormat("#.000000");
            System.out.println("pi = "+df.format(pi));
        }
    }

     

     

    3.13 求Fibonacci数列前40个数。这个数列有如下特点:第1、2个数为1、1。从第三个数开始,每个数是其前面两个数之和。即

    F1 = 1   (n=1)

    F2 = 1           (n=2)

    Fn = Fn-1+Fn-2              (n≥3)

    public class Demo3_13 {
        public static void main(String[] args) {
            long f1, f2;
            int i;
            f1 = f2 = 1;
            for (i = 1; i <= 20; i++) {
                System.out.print(f1+" "+f2+" ");

                if(i%2 == 0)
                    System.out.println();

                f1 = f1 + f2;
                f2 = f1 + f2;
            }
        }
    }

     

     

     

    3.14 找出100~200间的全部素数

     

    public class Demo3_14 {
        public static void main(String[] args) {
            int m, k, i, n = 0;
            boolean prime;
            for (m = 101; m <= 200; m=m+2) {
                prime = true;
                k = (int) (Math.sqrt(m));
                for (i = 2; i <= k; i++) {
                    if(m%i == 0) {
                        prime =false;
                        break;
                    }
                }
                if(prime) {
                    System.out.print(m+" ");
                    n++;
                }
                if(n%10 == 0){
                    System.out.println();
                }
            }

            System.out.println();
            System.out.println("共有"+n+"个素数");
        }
    }

     

     

    3.15 译密码。为使电文保密,往往按一定规律将电文转换成密码,收报人再按约定的规律将其译回原文。例如,可以按以下规律将电文变成密码:将字母A变成字母Ea变成e,即变成其后的第四个字母,W变成AX变成BY变成CZ变成D。字母按上述规律转换,非字母字符不变。如“Wonderful”转换为“Asrhivjyp”。输入一行字符,要求输出其对应的密码。

    import java.util.Scanner;

    public class Demo3_15 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);

            String s;
            s = sc.nextLine();

            char[] c = s.toCharArray();

            for (int i = 0; i < c.length; i++) {
                if ((c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z')) {
                    c[i] = (char)(c[i]+4);
                    if (c[i]>'Z'&&c[i]<=(char)('Z'+4)||c[i]>'z') {
                        c[i] = (char) (c[i]-26);
                    }
                }
                System.out.print(c[i]);
            }

        }
    }

     

     

    疑问:怎样输入字符串的方式输入一个字符数组类型的数据

  • 相关阅读:
    Android应用程序组件Content Provider简要介绍和学习计划
    本人其它博客
    Android应用程序组件Content Provider的启动过程源代码分析
    Android应用程序组件Content Provider应用实例
    Team Foundation Server 2010 Performance Tuning – Lessons learned
    Katapult:KDE 桌面辅佐序次
    Informix IDS 11系统打点(918考试)认证指南,第2局部系统活动监督(3)
    Amarok 1.4.6
    Informix IDS 11琐细管理(918测验)认证指南,第1部分IDS安设和设置(1)
    KTorrent 2.2 公布
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13556396.html
Copyright © 2011-2022 走看看