2018-08-03作业

2018-08-03  本文已影响0人  黄昭鸿
import java.util.Scanner;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;

public class TaxiChristmasTree99MultiplicationTable {
    public static void main(String[] args) {
        ifTaxi();
        printChristmasTree();
        multiplicationTable();
    }

    @SuppressWarnings("resource")
    public static void ifTaxi() {
        // 起步价//路程//总费用//税费
        double startPrice = 6.0, distance = 0.0, totalCost = 0.0, taxation = 1.0;
        int startTime = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("上车时间(整点):");// 2013-04-05 12:34:56
        if (scanner.hasNextInt()) {
            startTime = scanner.nextInt();
        } else {Console.log("输入时间点不是整数");System.exit(0);}

        // 路程
        System.out.print("路程:");
        if (scanner.hasNextDouble()) {
            distance = scanner.nextDouble();
        } else {Console.log("路程输入有误");System.exit(0);}

        // 起步价
        if (startTime <0 || startTime > 23) {
            Console.log("上车时间点输入有误");System.exit(0);
        } else if (startTime > 5 && startTime < 22) {           
            Console.log("白天" + startTime + "点,起步价6元");
        } else {
            //(startTime >= 0 && startTime < 6) || (startTime > 21 && startTime < 24)
            startPrice = 7.0;
            Console.log("晚上" + startTime + "点,起步价7元");
        }

        // 总费用
        if (distance < 0.0) {scanner.close();System.exit(0);}
        else if(distance<2.0) {totalCost = startPrice + taxation;}
        else{totalCost = startPrice + (distance - 2.0) * 1.5 + taxation;}
        
        System.out.println("总费用:¥" + totalCost + "(" + Convert.digitToChinese(totalCost) + ")");
        // scanner.close();//程序块单独运行时
    }

    // 九九乘法表
    public static void multiplicationTable() {
        Console.log("九九乘法表正序:");
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "x" + i + "=" + i * j + "\t");
            }
            Console.log();
        }
        /*
         * Console.log("九九乘法表倒序:"); for (int i = 9; i >= 1; i--) { for (int j =
         * 1; j <= i; j++) { System.out.print(j + "x" + i + "=" + i * j + "\t");
         * } Console.log(); }
         */
    }

    // 打印圣诞树,*=2x行号-1;" "=n行数-行号
    public static void printChristmasTree() {
        Console.log("输入圣诞树高度(<65):");
        Scanner scanner = new Scanner(System.in);
        byte n = 15, i = 1;// 行数,行号
        if (scanner.hasNextByte()) {
            n = scanner.nextByte();
            for (byte j = i; j <= n; j++) {
                for (byte k = 0; k < (n - j); k++) {System.out.print(" ");}
                for (byte j2 = 0; j2 < (2 * j - 1); j2++) {System.out.print("*");}
                Console.log();
            }
        } else {
            Console.log("输入有误");
        }
        scanner.close();
    }
}
上一篇下一篇

猜你喜欢

热点阅读