编程思维的训练(几个小题目)

          通过前面的学习我们可以完成以下几个题目!介意看完注释的题要求自己先编写再来看答案哦,和小白一起学习Java!相信自己,扬帆起航!

一、猜数字小游戏

package day07;

import java.util.Scanner;

/**
 * 1.需求:猜数字小游戏
 *   训练目标: while(true)自造死循环+break
 */
public class Guessing {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = (int)(Math.random()*1000+1); //1到1000
        System.out.println(num); //作弊

        while(true){ //自造死循环
            System.out.println("猜吧!");
            int guess = scan.nextInt();
            if(guess>num){
                System.out.println("猜大了");
            }else if(guess<num){
                System.out.println("猜小了");
            }else{
                System.out.println("恭喜你猜对了");
                break; //跳出循环
            }
        }
    }
}

二、生成N位验证码

package day07;

import java.util.Random;

/**
 * 2.需求: 随机生成N位验证码(大写字母、小写字母、数字)
 *   训练目标:随机数+数组+方法
 */
public class VerificationCode {
    public static void main(String[] args) {
        String code = generateVeriCode(6);
        System.out.println("验证码:" + code);
    }

    /** 生成N位验证码 */
    public static String generateVeriCode(int n){
        String code = ""; //验证码
        Random rand = new Random(); //随机数对象
        char[] chs = {'a','b','c','d','e','f','g','h','i','j','k',
                      'l','m','n','o','p','q','r','s','t','u','v',
                      'w','x','y','z','A','B','C','D','E','F','G',
                      'H','I','J','K','L','M','N','O','P','Q','R',
                      'S','T','U','V','W','X','Y','Z','0','1','2',
                      '3','4','5','6','7','8','9'}; //验证码字符可选范围(共62个)
        for(int i=1;i<=n;i++){ //n次
            int index = rand.nextInt(chs.length); //随机下标(0到61)
            code += chs[index]; //根据随机下标获取对应字符并拼接到code中
        }
        return code;
        /*
                        code=""
          i=1  index=0  code="a"
          i=2  index=61 code="a9"
          i=3  index=25 code="a9z"
          i=4  index=28 code="a9zC"
          i=5
         */
    }
}

三、找2-100之间的所有素数

package day07;

/**
 * 3.需求:找到2到100之间的所有素数(质数)
 *       素数为只能被1和它本身整除
 *   训练目标: 通过boolean的flag打标记(3步)
 */
public class PrimeNumber {
    public static void main(String[] args) {
        //带数(2/3/4/5/6/7/8)
        for(int num=2;num<=100;num++){
            boolean flag = true; //假设每个num都是true
            for(int i=2;i<=num/2;i++){
                if(num%i==0){
                    flag = false;
                    break;
                }
            }
            if(flag){
                System.out.print(num+"\t");
            }
        }

四、飞机机票的价格

      

package day07;

import java.util.Scanner;

/**
 * 4.需求:机票价格按照季节(淡季、旺季)、舱位(头等舱、商务舱、经济舱)收费
 *   要求:
 *     输入机票原价、月份和舱位,实现不同的折扣
 *     ---旺季(5月到10月)时,头等舱9折,商务舱85折,经济舱8折
 *     ---淡季(11月到来年4月)时,头等舱7折,商务舱65折,经济舱6折
 *   训练目标: 分支结构
 */
public class CalAirPrice {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入机票原价:");
        double price = scan.nextDouble();
        System.out.println("请输入月份:");
        int month = scan.nextInt();
        System.out.println("请选择舱位: 1.头等舱 2.商务舱 3.经济舱");
        int type = scan.nextInt();

        double finalPrice = calFinalPrice(price,month,type); //计算折扣后金额
        if(finalPrice!=-1){ //数据合法
            System.out.println("机票的最终价格为:"+finalPrice);
        }
    }

    /** 根据原价、月份和舱位,计算机票的最终价格 */
    public static double calFinalPrice(double price,int month,int type){
        double finalPrice = 0.0; //最终价格
        //只要数据输入不合法,都统一返回-1
        if(price<0){
            System.out.println("机票原价输入错误");
            return -1;
        }
        if(month<1 || month>12){
            System.out.println("月份输入错误");
            return -1;
        }
        if(type<1 || type>3){
            System.out.println("舱位输入错误");
            return -1;
        }

        //程序能走到这,说明数据一定是合法的
        if(month>=5 && month<=10){ //旺季
            switch(type){ //根据舱位类型做不同折扣
                case 1:
                    finalPrice = price*0.9;
                    break;
                case 2:
                    finalPrice = price*0.85;
                    break;
                case 3:
                    finalPrice = price*0.8;
            }
        }else{ //淡季
            if(type==1){
                finalPrice = price*0.7;
            }else if(type==2){
                finalPrice = price*0.65;
            }else{
                finalPrice = price*0.6;
            }
        }
        return finalPrice;
    }
}

五、求平均数

package day07;

import java.util.Scanner;

/**
 * 5.需求:<<主持人大赛>>有N位评委给选手评分,分数范围为0到100之间的浮点数
 *       选手的最终得分为: 去掉最高分和最低分之后的N-2位评委的平均分
 *   训练目标:分支+循环+数组+方法
 */
public class CalTotalAvg {
    public static void main(String[] args) {
        double[] scores = inputData(6); //1)录入评委的评分
        double avg = calAvg(scores); //2)计算平均分
        System.out.println("平均分为:"+avg);
    }

    /** 录入N位评委的评分 */
    public static double[] inputData(int count){
        double[] scores = new double[count]; //评分数组
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<scores.length;i++){
            System.out.println("请录入第"+(i+1)+"位评委的分数:");
            scores[i] = scan.nextDouble();
        }
        return scores;
    }

    /** 计算平均分 */
    public static double calAvg(double[] scores){
        double total = 0.0; //总分
        double max = scores[0]; //假设第1个元素为最高分
        double min = scores[0]; //假设第1个元素为最低分
        for(int i=0;i<scores.length;i++){
            if(scores[i]>max){ //找最高分
                max = scores[i];
            }
            if(scores[i]<min){ //找最低分
                min = scores[i];
            }
            total += scores[i]; //累加所有评分
        }
        //计算平均分--总分减掉最高分和最低分之后,再除以(评委数-2)
        double avg = (total-max-min)/(scores.length-2);
        return avg;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值