Java——查漏补缺(1) 备份

 1、'/t'——制表符

        在打印的时候,把前面字符串的长度补齐到0,或者8的整数倍。最少补1个空格,最多不8个空格。使用如下:

public class Demo01 {
    public static void main(String[] args) {
        System.out.println("name" + '\t' + "age");
        System.out.println("lj" + '\t' + "45");
        System.out.println("lisi" + '\t' + "35");
    }
}

运行结果如下:

2、任意进制转十进制

        公式:系数 * 基数的权次幂,最后相加

                系数:就是每一位上的数字。

                基数:当前进制数

                权:从右往左,依次为0 1 2 3 4 5 ....

        例如:二进制转十进制

        

        八进制转十进制

3 JDK12:Switch新特性

   

public class SwitchNew {
    public static void main(String[] args) {
        //从JDK12 以后,Switch新特性
        int number = 1;
        switch (number){
            case 1 -> System.out.println(1);

            case 2 -> System.out.println(2);

            case 3 -> System.out.println(3);

            default ->  System.out.println("没有这个值");
            
        }
    }
}

      

4 算法小题

        需求:珠穆朗玛峰高度是 8844430毫米,假设有一张足够大的纸,它的厚度是0.1毫米 请问,需要折叠多少次,可以折成珠穆朗玛峰的高度。分别使用for循环和while循环实现,代码如下:

public class LoopTest {
    public static void main(String[] args) {
        forTest();
       // whileTest();
    }

    public static void forTest(){
        double height = 8844430.0;
        double paper = 0.1D;
        int count = 0;
        for (int i = 1; ; i++) {

            if(paper > height){
                break;
            }
            paper *= 2;
            count ++;
        }
        System.out.println("循环次数:" + count);
        System.out.println("纸的厚度:" + paper);
    }

    public static void whileTest(){
        double height = 8844430.0;
        double paper = 0.1D;
        int count = 0;
        while(paper < height){
            paper *= 2;
            count ++;
        }
        System.out.println("循环次数:" + count);
        System.out.println("纸的厚度:" + paper);
    }
}

         回文整数:

/*
    需求:输入一个整数x,如果x是一个回文整数,打印true;否则,返回false
    解释:回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
    例如:121是回文,而123不是
 */
public class LoopTest02 {
    public static void main(String[] args) {
        //核心思路:把数字倒过来跟原来的数字进行比较
        int x = 121;
        int temp = x;
        int num = 0;
        while(x != 0){
            int a = x % 10;
            x = x / 10;
            num = num * 10 + a;
        }
        System.out.println(num);
        if(num == temp){
            System.out.println(true);
        }else {
            System.out.println(false);
        }

    }
}

        逢7过:

/*
    逢7过:从任意一个数字开始,遇到的数字是包含7或者是7的倍数,说过
    需求:在控制台打印出1-100之间的满足逢7过的数据
 */
public class LoopTest03 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            //判断当前数字是否包含7
            int ge = i % 10;
            int shi = i / 10;
            if((ge == 7 || shi == 7) || (i % 7 == 0)){
                System.out.println(i);
            }
        }
    }
}

       卖飞机票

        需求:机票价格按照淡季旺季、头等舱和经济舱收费,输入机票原价、月份和头等舱或经济舱。

        按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到第二年4月)头等舱7折,经济舱6.5折。

/**
 * @author liujie
 * @create 2024-07-23 12:11
 *需求:机票价格按照淡季旺季、头等舱和经济舱收费,输入机票原价、月份和头等舱或经济舱。
 *
 * 按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到第二年4月)头等舱7折,经济舱6.5折。
 */
public class Demo01 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        //请输入机票原价
        System.out.println("请输入机票原价");
        int original_price = scanner.nextInt();
        System.out.println("请输入月份");
        //请输入机票月份
        int month = scanner.nextInt();
        //校验
        if(!(month >=1 && month <=12)){
            System.out.println("输入月份不合法,请重新输入");
            return;
        }
        System.out.println("请输入机舱类型");
        //请输入头等舱或经济舱
        String type = scanner.next();
        //校验
        if(!(type.equals("头等舱") || type.equals("经济舱"))){
            System.out.println("机舱类型不对,请重新输入");
            return;
        }
        double result = calc(original_price,month,type);
        System.out.println("计算后的机票价格是:"+result);

    }

    public static double calc(int price,int month,String type){
        double result = 0.0d;
        if(month >=5 && month <=10){
            if(type.equals("头等舱")){
                result = price * 0.9;
            }else{
                result = price * 0.85;
            }

        }else{
            if(type.equals("头等舱")){
                result = price * 0.7;
            }else{
                result = price * 0.65;
            }
        }
        return result;
    }
}

找质数:

需求:判断101~200之间有多少个质数,并打印。

/**
 * @author liujie
 * @create 2024-07-23 12:35
 * 输出101~200之间的质数
 */
public class Demo02 {
    public static void main(String[] args) {

        int count = 0;
        for (int i = 101; i <201 ; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if(i % j == 0){
                    //System.out.println(i + "不是质数");
                    flag = false;
                    break;
                }
            }
            if(flag){
                System.out.println(i+"是质数");
                count ++;
            }
        }
        System.out.println("101~200一共:" + count + "个质数");
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geminigoth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值