java 第一天 运算符、表达式、语句

一个&,一个|为短路与、或,&表示第一个判断为false时,则后面的判断不再进行,|表示第一个判断时为true时,后面的判断不再进行。

习题1、打印1~10000范围内的水仙数(水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153))。

public class Narcissistic {

    public static void main(String[] args) {
        int num = 0; //计算有多少个水仙花数
        int sum = 0; //每个位上三次方的总数
        for (int i = 100; i <= 1000; i++) { //三位数的水仙花数
            int x = i;//保存i
            while (x != 0) {
                int y = x % 10;
                sum = sum + y * y * y;

                //System.out.println(sum);
                x = x / 10;
            }

            if (x == 0) {
                if (sum == i) {
                    System.out.println(i);
                    sum = 0;
                } else {
                    sum = 0;
                }
            }
            //System.out.println(sum);
        }
        for (int i = 1000; i <= 10000; i++) { //四位数的水仙花数
            int x = i;//保存i
            while (x != 0) {
                int y = x % 10;
                sum = sum + y * y * y * y;

                //System.out.println(sum);
                x = x / 10;
            }

            if (x == 0) {
                if (sum == i) {
                    System.out.println(i);
                    sum = 0;
                } else {
                    sum = 0;
                }
            }
        }
    }

习题2 两个整数交换;

 public class Change {

   
    public static void main(String[] args) {

        int a = 100;
        int b = 222;
        int c;

        c = a;//插入一个C的交换方法
        a = b;
        b = c;
           
        a=a+b;//数学交换,觉得不怎样
        b=a-b;
        a=a-b;
       
        System.out.println("a"+a+"   ,b"+b);
        // TODO code application logic here
    }

}
   习题3 给3个数,求出三个数中最大的一个;

public class FindTheMax {

   
    public static void main(String[] args) {
       
        int a =1;
        int b =11;
        int c =111;
       
        int max=a;//设置一个顶点
       
        if(b>max){
            max =b;
        }
        if(c>max){
            max=c;
        }
        System.out.println(max);
        // TODO code application logic here
    }
}

习题4 判断某数是否可以被3,5,7同时整除;//懒得弄,直接算出来

public class DivideBy357 {

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

    }

}
习题5 算出100~200的累加数

public class Sum {

   
    public static void main(String[] args) {
        // TODO code application logic here
        int i = 100;
        int sum = 0;
        while (i <= 200) {
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }

}

 

习题6 算出1!+2!+....+20!的值;

public class Factorial {

   
    public static void main(String[] args) {
        // TODO code application logic here
        BigInteger sum = new BigInteger("0");//总数
        BigInteger y = new BigInteger("1");//单个数的阶乘
        BigInteger i = new BigInteger("1");//递归相加
        BigInteger x =new BigInteger("1");//加1,因为BigInternet是String类型
        BigInteger n = new BigInteger("20");//终点
        while(i.compareTo(n)<=0){
            sum=sum.add(y);
            i=i.add(x);
            y=y.multiply(i);
        }
       
        System.out.println(sum);
    }

}

习题7 用for打印出*金字塔;

public class Pyramid {

   
    public static void main(String[] args) {
        // TODO code application logic here
        int x = 4;
        for (int i = 1; i <= 5; i++) {

            for (int h = x; h > 0; h--) {
                System.out.print(" ");
            }
            x--;
            for (int k = i; k != 0; k--) {

                System.out.print("*");
            }
            System.out.println();
        }
    }

}
习题8 求出13-23+33-...+973-983+993-1003

public class JavaTest {

   
    public static void main(String[] args) {

        boolean positive = false;
        int sum = 13;
        for (int x = 23; x <= 1003; x = x + 10) {
            if (positive == true) {
                sum = sum + x;
                positive = false;
            } else {
                sum = sum - x;
                positive = true;
            }

        }
        System.out.println("sum is" + sum);
        // TODO code application logic here
    }

}

第一次打这么代码,挺爽的哈哈,阶乘累加,高精度没想到,一开始写错了都不知道,╮(╯▽╰)╭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值