第14天

一、卖飞机票

 

package com.lianxi.www;

import java.util.Scanner;

public class 找飞机票 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入机票的原价");
        int ticket = sc.nextInt();
        System.out.println("请输入当前月份");
        int month = sc.nextInt();
        System.out.println("请输入0或1 其中0代表头等舱,1代表经济舱");
        int zuo = sc.nextInt();
        if (month >= 5 && month <= 10) {
            if (zuo == 0) {
                ticket = (int) (ticket * 0.9);
            } else {
                ticket = (int) (ticket * 0.85);
            }
        } else if ((month >= 1 && month <= 5) || (month >= 11 && month <= 12)) {
            if (zuo == 0) {
                ticket = (int) (ticket * 0.7);
            } else {
                ticket = (int) (ticket * 0.65);
            }
        }
        System.out.println("机票价格为:" + ticket + "元");
    }
}

 我们可以使用方法来重载函数进行优化代码。

package com.lianxi.www;

import java.util.Scanner;

public class 找飞机票 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入机票的原价");
        int ticket = sc.nextInt();
        System.out.println("请输入当前月份");
        int month = sc.nextInt();
        System.out.println("请输入0或1 其中0代表头等舱,1代表经济舱");
        int zuo = sc.nextInt();
        if (month >= 5 && month <= 10) {
            ticket = getPrice(ticket, zuo, 0.9, 0.85);
        } else if ((month >= 1 && month <= 5) || (month >= 11 && month <= 12)) {
            ticket = getPrice(ticket, zuo, 0.7, 0.65);
        } else {
            System.out.println("您输入的月份数据不合法");
        }
        System.out.println("机票价格为:" + ticket + "元");
    }

    public static int getPrice(int ticket, int zuo, double v1, double v2) {
        if (zuo == 0) {
            ticket = (int) (ticket * v1);
        } else {
            ticket = (int) (ticket * v2);
        }
        return ticket;
    }
}

 二、求101~200之间的素数。

package com.lianxi.www;

//求101~200之间素数的和 并打印出每个素数
public class 求素数 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 101; i <= 200; i++) {
            boolean flag = getResult(i);
            if (flag == true) {
                System.out.println(i);
                count++;
            }
        }
        System.out.println("质数的总和为:" + count);
    }

    public static boolean getResult(int i) {
        for (int j = 2; j < i; j++) {
            if (i % j == 0) {
                return false;
            }
        }
        return true;
    }
}
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
质数的总和为:21

 三、随机验证码

package com.lianxi.www;

import java.util.Random;

public class 输出一个随机验证码 {
    public static void main(String[] args) {
        Random r = new Random();
        char[] chs = new char[52];
        for (int i = 0; i < chs.length; i++) {
            if (i < 26) {
                chs[i] = (char) (97 + i);
            } else {
                chs[i] = (char) (65 + i-26);
            }
        }
        for (int i = 0; i < 4; i++) {
            System.out.print(chs[r.nextInt(52)] + " ");
        }
        System.out.println(r.nextInt(10));
    }
}
U i t D 3
U Q A X 1
r q h m 7
X X a H 9

 

posted @ 2023-07-15 11:38  七安。  阅读(12)  评论(0编辑  收藏  举报