阶段小练习

练习(变量、控制语句、数组)

[练习题目]

1、定义一个 int 变量(unfinishedCount)存放当前你作业未完成次数,定义一个 String 变量(result)存放三元运算符返回的结果,达到 3 次或以上就存放“开除”,否则存放“还在”。

package cn.wolfcode.day01;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @program: wintervacationhomework
 * @author: Mr.Gao
 * @Date: 2021/02/02 19:06
 */
public class Demo01 {
    public static void main(String[] args) {

        // 定义一个变量 存储未完成作业的次数
        int unfinishedCount = 0;
        // 定义一个变量用来存放三元运算符返回的结果.
        String result = "";
        System.out.println("请输入你未完成作业的次数");
        // 创建一个控制台接收器。
        try {
            Scanner s = new Scanner(System.in);
            int i = s.nextInt();
            // 判断 完成的次数不能输入负数.
            if (i < 0) {
                System.out.println("不能输入负数");
                return;
            }
            unfinishedCount = i;
            result = (unfinishedCount >= 3) ? "开除" : "还在";

        } catch (InputMismatchException e) { // 次数应只能输入一个整数.
            throw new InputMismatchException("输入的数字不合规范");
        }
        // 结果应该在外面输出
        System.out.println(result);
    }
}

输出

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、定义一个 int 变量存放一个三位数的正数,分别求出百位数上的值、十位数上的值和个位数上的值。 例如: 432 百位4 十位3 个位2

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 19:23
*/
public class Demo02 {

   public static void main(String[] args) {

       // 创建一个 int 变量存储三位数的正数
       int count = 432;

       // 求出百位上的数
       int hundred = count / 100;
       System.out.println("百位上的数" + hundred);

       // 求十位上的数
       int ten = (count % 100) / 10;
       System.out.println("十位上的数" + ten);

       // 求个位上的数
       int unit = count % 10;
       System.out.println("个位上的数" + unit);

   }
}

输出

百位上的数4
十位上的数3
个位上的数2

3、根据课程编号打印输出对应的课程名称。(使用 if , switch 2种方式实现)

​ 编号 : 课程

分别为 1 : java , 2 : php 3 : html 4 : ios

package cn.wolfcode.day01;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @program: wintervacationhomework
 * @author: Mr.Gao
 * @Date: 2021/02/02 19:36
 */
public class Demo03 {

    public static void main(String[] args) {

        // 创建一个变量来存储课程的编号
        int classNum = 0;
        System.out.println("请输入你想要查询的课程");
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        if (i < 1 || i > 4) {
            System.out.println("没有这个课程");
            return;
        }
        classNum = i;

        System.out.println("编号  :  课程");
        // 使用 if
        if (classNum == 1) {
            System.out.println("1   :   java");
        } else if (classNum == 2) {
            System.out.println("2   :   php ");
        } else if (classNum == 3) {
            System.out.println("3   :   html");
        } else {
            System.out.println("4   :   ios");
        }

        // 使用switch
        switch (classNum) {
            case 1: {
                System.out.println("1   :   java");
                break;
            }
            case 2: {
                System.out.println("2   :   php ");
                break;
            }
            case 3: {
                System.out.println("3   :   html");
                break;
            }
            default: {
                System.out.println("4   :   ios");
                break;
            }
        }

        // 使用hashmap
        HashMap<Integer, String> aClass = new HashMap<>(4);
        aClass.put(1, "java");
        aClass.put(2, "php");
        aClass.put(3, "html");
        aClass.put(4, "ios");

        // 根据key值 来找到对应的values值.
        System.out.println(i + "   :" + "   " + aClass.get(i));
    }
}

输出

在这里插入图片描述

5、定义两个变量用来保存 100 以内的 偶数的总和 与 奇数的总和 , 最后打印出 2 个总额的值分别是多少 (while 实现)

public class Demo04 {

   public static void main(String[] args) {

       // 定义两个变量
       int evenNum = 0;
       int oddNum = 0;

       int i = 0;
       // 定义循环条件 100以内不包括100
       while (i < 100) {

           // 循环体
           if (i % 2 == 0) {
               evenNum += i;
           } else {
               oddNum += i;
           }
           // 控制循环的变量
           i++;
       }
       System.out.println(evenNum);
       System.out.println(oddNum);
   }
}

输出

2450
2500

6、求整数1~100的累加值,但要求跳过所有个位为3的数。( while 实现) 如: 13 ,23 …等 不参与累加计算

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 20:02
*/
public class Demo06 {

   public static void main(String[] args) {
       // 创建一个变量来存储累加值
       int addNum = 0;

       int i = 0;
       while (i<=100){
           boolean flag = (i == 3) | (i % 10 == 3);
           if (!flag){
               addNum += i;
           }
           i++;
       }
       System.out.println(addNum);
   }
}

输出

4570

7、本金10000元存入银行,年利率是百分之三 (0.03) 。每过1年,将本金和利息相加作为新的本金。计算5年后总金额是多少?(while实现)

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 20:10
*/
public class Demo07 {

   public static void main(String[] args) {

       // 创建一个变量存储本金
       double principal = 10000;

       int i = 1;
       while (i <= 5){
           principal = principal + principal*0.03;
           i++;
       }
       System.out.println(principal);
   }

}

输出

11592.740743

8、一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 17:17
* 8、一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
* <p>
* 0.08
* 0.16
* 0.32
*/
public class Demo08 {
   public static void main(String[] args) {

       // 分析 纸张每次折叠之后 厚度是前一次的2倍
       // 纸的厚度是0.08mm 珠穆朗玛峰的高度是8848.13m 即为8848130mm

       // 创建一个变量存储纸张的厚度
       double thickness = 0.08;
       double high = 8848130;
       double afterFold = 1;
       int i = 2;
       while (true) {

           afterFold = thickness * Math.pow(2, i);
           if (afterFold >= high)break;
           i++;
           System.out.println("次数:" + i+" "+  "纸张厚度:" + afterFold );
       }
       System.out.println(i);


       // 调用递归方法
       double fold = fold(0.08, 8848130, 1);
       System.out.println(fold);

   }

   /**
    * 创建一个递归方法
    * 参数类型 fold(纸张厚度 , 需要超越的高度 , 开始的次数)
    * eg.fold(0.08,884800130,1)
    */
   public static int fold(double thickness, double high, int i) {


       double afterFold = thickness * Math.pow(2, i);
       i++;
       // 结束条件
       if (afterFold >= high) {
           return i;
       } else {

           return fold(thickness, high, i);
       }
   }


}

输出

27
28.0

9、计算5的阶乘 5!的结果是?

(一个正整数的 阶乘(英语: factorial)是所有小于及等于该数的 正整数的 积,并且0的阶乘为1。自然数n的阶乘写作n!)

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 20:34
*/
public class Demo09 {

   public static void main(String[] args) {

       int result = 1;
       for (int i = 5; i >= 1; i--) {
           result = result * i;
       }
       System.out.println(result);

       // 调用递归的方法
       System.out.println(factorial(5));
   }

   /**
    * 创建一个递归方法
    */
   public static int factorial(int n) {
       
       // 创建一个判断结束的语句
       if (n == 1) {
           return 1;
       }
       return n* factorial(n-1);
   }
}

输出

120
120

10、打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 21:35
*/
public class Demo10 {

   public static void main(String[] args) {

       // 打印出所有的水仙花数 即从 100-999 之间进行遍历
       // 创建变量分别存储 百位数 十位数 个位数
       int hundred = 0;
       int ten = 0;
       int unit = 0;

       for (int i = 100; i <= 999; i++) {
           hundred = i / 100;
           ten = (i % 100) / 10;
           unit = i % 10;
           boolean flag = Math.pow(hundred, 3) + Math.pow(ten, 3) + Math.pow(unit, 3) == i;
           if (flag) {
               System.out.print(i + " ");
           }
       }
   }
}

输出

153 370 371 407 

11、我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的:5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。

package cn.wolfcode.day01;

/**
* @program: wintervacationhomework
* @author: Mr.Gao
* @Date: 2021/02/02 21:45
*/
public class Demo11 {

   public static void main(String[] args) {

       // 创建变量保存 公鸡,母鸡,雏鸡
       // 公鸡
       int cock = 0;
       // 母鸡
       int hen = 0;
       int chick = 0;

       //分析:
       // 只要我知道 有多少只公鸡 和多少只母鸡 就知道有多少只雏鸡
       // 公鸡5文钱一只 所以100文钱中 公鸡的数量不可能超过20只
       // 母鸡3文钱一只 所以100文钱中 母鸡的数据不可能超过33只
       // 可以创建两个遍历进行嵌套

       for (hen = 0; hen < 33; hen++) { // 公鸡的数量
           for (cock = 0; cock < 20; cock++) { // 母鸡的数量

               // 雏鸡
               chick = 100 - cock - hen;
               //int chickCash = (int) (chick*(1.0/3));
               if (cock * 5 + hen * 3 + chick *(1.0/3)  == 100) {
                   System.out.print("公鸡的数量有" + cock + "只\t");
                   System.out.print("母鸡的数量有" + hen + "只\t");
                   System.out.print("雏鸡的数量有" + chick + "只");
                   System.out.println();
               }
           }
       }
   }
}

输出

公鸡的数量有12只	母鸡的数量有4只	雏鸡的数量有84只
公鸡的数量有8只	母鸡的数量有11只	雏鸡的数量有81只
公鸡的数量有4只	母鸡的数量有18只	雏鸡的数量有78只
公鸡的数量有0只	母鸡的数量有25只	雏鸡的数量有75
         if (cock * 5 + hen * 3 + chick *(1.0/3)  == 100) {
                System.out.print("公鸡的数量有" + cock + "只\t");
                System.out.print("母鸡的数量有" + hen + "只\t");
                System.out.print("雏鸡的数量有" + chick + "只");
                System.out.println();
            }
        }
    }
}

}


输出

~~~java
公鸡的数量有12只	母鸡的数量有4只	雏鸡的数量有84只
公鸡的数量有8只	母鸡的数量有11只	雏鸡的数量有81只
公鸡的数量有4只	母鸡的数量有18只	雏鸡的数量有78只
公鸡的数量有0只	母鸡的数量有25只	雏鸡的数量有75只
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值