目录
一,出租车计费训练任务(分支语句)
题目描述
本次任务一起来解决出租车计费问题。某市出租车计费标准如下图所示, 请根据此标准完成一个出租车计费模拟功能,能够计算总费用和列出产生费用 项目详细情况说明,帮助出租车师傅和乘客了解计费标准。
结合上述表格,可以得出:总车费=里程费用+低速行驶费(或者等候费) +预约叫车服务费+空驶费+夜间收费+燃油附加费。需要收集的数据有:里程数、 低速行驶时长(早晚高峰期行驶时长和其他时间段行驶时长)、是否预约叫车 (按四小时为标准)、开始乘坐出租车时间、出租车到达终点站时间,结合这 些数据和表中提供的标准就可以使用程序进行计算总车费了。
参考代码
由于题目描述的并不是很具体,这里只是简单的实现了部分功能,尚有很大不足,仅供参考。
package com.kaikeba.demo;
import java.util.Scanner;
public class Demo1 {
public static boolean isPeak(int time) {
if((time >= 7 * 60 && time < 9 * 60) || (time >= 17 * 60 && time < 19 * 60)) {
return true;
}
return false;
}
public static void main(String[] args) {
float totalCost = 0.0f;
Scanner input = new Scanner(System.in);
// 里程费用
System.out.println("请输入总里程数:");
float dis = input.nextFloat();
totalCost += 13;
totalCost += (dis > 3 ? (dis - 3) * 2.3f : 0.0f);
// 低速行驶费
System.out.println("请输入低速行驶时长/分钟(高峰期):");
int time = input.nextInt();
totalCost += (time % 5 == 0 ? time / 5 * 2.3 * 2 : (time / 5 + 1) * 2.3 * 2); // 不足5分钟按5分钟计算
System.out.println("请输入低速行驶时长/分钟(非高峰期):");
time = input.nextInt();
totalCost += (time % 5 == 0 ? time / 5 * 2.3 : (time / 5 + 1) * 2.3);
// 预约叫车费
System.out.println("是否预约叫车?(Y / N)");
String s = input.next();
if(s == "Y") {
System.out.println("预约4小时以内?(Y / N)");
s = input.next();
totalCost += (s == "Y" ? 5 : 6);
}
// 空驶费
if(dis > 15) {
System.out.println("往返载客?(Y / N)");
s = input.next();
if(s == "N") {
totalCost += (dis - 15) * 2.3 * 0.5;
}
}
// 夜间收费
System.out.println("23:00至次日5:00运营公里数");
dis = input.nextFloat();
totalCost += dis * 2.3 * 0.2;
// 燃油费
totalCost += 1;
System.out.println("总费用为:" + totalCost);
}
}
二,人工智障训练任务(循环)
描述
人工智能的概念刚兴起时,网上流传了一段价值一个亿的代码,如下图:
运行效果如下图:
代码
package com.kaikeba.demo;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String question;
while(true) { // 死循环 不断地读取数据
question = input.next(); // 读取下一个字符串
question = question.replace("吗", ""); // 吗 替换为 空
question = question.replace("我", "我也"); // 我 替换为 我也
question = question.replace("?", "!"); // ? 替换为 !
System.out.println(question); // 输出改造后的字符串
}
}
}
三,流程控制的逻辑训练任务(分支+循环综合)
1,计算应缴金额
描述
商场根据会员积分打折:
2000 分以内打 9 折,
4000 分以内打 8 折,
8000 分以内打 7.5 折,
8000 分以上打 7 折,
使用 if-else-if 结构,实现手动输入购物金额和积分, 计算出应缴金额
代码
package com.kaikeba.demo;
import java.util.Scanner;
public class Demo3_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入购物金额:");
float cost = input.nextFloat();
System.out.println("输入积分:");
int score = input.nextInt();
if(score < 2000) {
cost *= 0.9f;
}else if(score < 4000) {
cost *= 0.8f;
}else if(score < 8000) {
cost *= 0.75f;
}else {
cost *= 0.7f;
}
System.out.println("最终消费金额为:" + cost);
}
}
2,计算该年该月天数
描述
一年中有 12 个月,而每个月的天数是不一样的。其中大月 31 天,分别为 1,3,5,7,8,10,12 月,小月 30 天,分别 为 4,6,9,11 月。还有二月比较特殊,平 年的二月只有 28 天,而闰年的二月有 29 天,由用户在控制台输入年份和月份, 程序计算该年该月的天数。
代码
package com.kaikeba.demo;
import java.util.Scanner;
public class Demo3_2 {
public static boolean isLeapYear(int y) {
if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) {
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入年份:");
int y = input.nextInt();
System.out.println("输入月份:");
int m = input.nextInt();
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(y + "年" + m + "月共有31天");
break;
case 2:
if(isLeapYear(y)) {
System.out.println(y + "年" + m + "月共有29天");
}else {
System.out.println(y + "年" + m + "月共有28天");
}
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(y + "年" + m + "月共有30天");
default :
System.out.println("输入错误");
break;
}
}
}
3,图形打印任务
描述
在控制台中,编写三个 Demo,分别输出如下图形:
代码
package com.kaikeba.demo;
public class Demo3_3_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 1; i <= 5; i++) {
for(int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
//
package com.kaikeba.demo;
public class Demo3_3_2 {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 5; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
//
package com.kaikeba.demo;
public class Demo3_3_3 {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) { // 行数
for(int j = 5; j > i; j--) { // 空格数
System.out.print(" ");
}
for(int j = 0; j < 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
4,打印九九乘法表
描述
代码
package com.kaikeba.demo;
public class Demo4 {
public static void main(String[] args) {
System.out.println("乘法口诀表:");
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + j * i + "\t");
}
System.out.println();
}
}
}
5,打印三位数中的所有水仙花数
描述
所谓“水仙花数”即一个整数满足其值等于各个数位的立方和。 如: 153 是一个水仙花数,因为 153= 1³+5³+3³
代码
package com.kaikeba.demo;
public class Demo5 {
public static void main(String[] args) {
for(int num = 100; num < 1000; num++) { // 遍历所有三位数
int tem = num, sum = 0;
for(int i = 0; i < 3; i++) { // 取出每个位上的值
sum += (tem % 10) * (tem % 10) * (tem % 10);
tem /= 10;
}
if(sum == num) {
System.out.println(num);
}
}
}
}