Java练习(1.验证码2.素数3.飞机票)

 1.随机产生一个五位验证码

package com.itheima.tset;

import java.util.Random;

/*
  定义方法实现随机产生一个五位的验证码
  验证码格式:
  长度为5,前4位是大写或小写字母,最后一位为数字
*/
public class Test3 {
    public static void main(String[] args) {
        char[] chs = new char[52];
        for (int i = 0; i < chs.length; i++) {
//          利用ASCII码表
            if (i <= 25) {
                //小写97~122
                chs[i] = (char) (97 + i);//char也要括起来!!!
            } else {
                //大写65~90
                chs[i] = (char) (65 + i - 26);
            }
        }
//      定义一个字符串类型的变量,记录最终结果
        String result = "";

        Random r = new Random();
//      前面四个字母
        for (int i = 0; i < 4; i++) {
            int temp = r.nextInt(chs.length);
            result = result + chs[temp];
        }
//      最后一个数字
        int temp2 = r.nextInt(10);
//      生成最终结果
        result = result + temp2;
//      打印最终结果
        System.out.println(result);
    }
}

 2.判断101~200内有多少个素数,并输出

package com.itheima.tset;
//判断101~200有多少素数,并输出所有素数
public class Test2 {
    public static void main(String[] args) {
        int count=0;
        for(int i=101;i<=200;i++){
            boolean temp=true;
            for(int j=2;j<=GetSquare(i);j++){
                if(i%j==0){
                    count++;
                    temp=false;
                    break;
                }
            }
           if(temp){
                System.out.print(i+" ");
            }
        }
        System.out.println("101到200之间有"+(100-count)+"个素数");
    }
    public static int GetSquare(int i){
        int square=1;
        for(int k=1;k<=i;k++){
            if(k*k==i){
               square=k;
               break;
            } else if (k*k>i) {
                square=k;
                break;
            }
        }
        return square;
    }
}

3.卖飞机票

package com.itheima.tset;

import java.util.Scanner;

//卖飞机票
public class Test1 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入机票原价:");
        double price=sc.nextInt();
        System.out.println("请输入月份(1~12):");
        int month=sc.nextInt();
        System.out.println("选择头等舱请输入1,选择经济舱请输入2:");
        int choice=sc.nextInt();

        double newPrice=CalcPrice(price,month,choice);
        System.out.println("最终机票的价格为:"+newPrice);
    }
    public static double CalcPrice(double price,int month,int choice){
       if(choice==1){
           if(month>=5&&month<=10){
               price=0.9*price;
           }
           else price=0.7*price;
       } else if (choice==2) {
           if(month>=5&&month<=10){
               price=0.85*price;
           }
           else price=0.65*price;
       }
       return price;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值