目录
XZK-JavaEE 技术方向-10101003-选择结构训练任务
1、某市出租车,起步价(2 公里以内)为 8 元,超过 2 公里的按照每公里 4.5 元计算。要求根据路程计算费用。
3、要求输入月份,判断该月所处的季节并输出季节(假设:12、1、2 月为冬季,依次类推)
XZK-JAVA-支线任务-010103-002- 流程控制的逻辑训练任务(分支+循环综合)
3、图形打印任务在控制台中,编写三个 Demo,分别输出如下图形:
5、打印三位数中的所有水仙花数所谓“水仙花数”即一个整数满足其值等于各个数位的立方和。如: 153 是一个水仙花数,因为 153= 1³+5³+3³
人工智能的概念刚兴起时,网上流传了一段价值一个亿的代码,如下图
XZK-JavaEE 技术方向-10101003-选择结构训练任务
1、某市出租车,起步价(2 公里以内)为 8 元,超过 2 公里的按照每公里 4.5 元计算。要求根据路程计算费用。
public static void main(String[] args) {
System.out.println("请输入路程为:");
Scanner input = new Scanner(System.in);
double s = input.nextDouble();
if(s<=0) {
System.out.println("你输入的路程是个锤子");
}else if(s<2){
System.out.println("路程计算费用为8元");
}else{
System.out.println("路程计算费用为"+s*4.5+"元");
}
}
}
2、输入年份,判断输入的年份是否是闰年。(闰年的条件是能被 4 整除,但不能被 100 整除;或能被 400 整除。)
public static void main(String[] args) {
System.out.println("请输入年份:");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if (i%4==0&&i%100!=0||i%400==0) {
System.out.println("你输入的是闰年");
}else {
System.out.println("你输入的不是闰年");
}
}
}
3、要求输入月份,判断该月所处的季节并输出季节(假设:12、1、2 月为冬季,依次类推)
public static void main(String[] args) {
System.out.println("请输入月份:");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
switch(i) {
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("你输入的不是月份");
break;
}
}
}
4、根据《国家电网销售电价表》,居民生活用电按 3 个梯度收费:月用电量 150 千瓦时及以下部分,每千瓦时 0.43 元,151—400 千瓦时部分为 0.45元,401 千瓦时以上部分为 0.52 元,请编写程序,当输入用户的用电量时,计算出所需付的费用。
public static void main(String[]args) {
System.out.println("请输入用户用电量:");
Scanner input = new Scanner(System.in);
int i =input.nextInt();
if(i<0) {
System.out.println("你输入的用户用电量有误");
}else if (i<=150) {
System.out.println("你输入的用户用电量为"+i*0.43+"元");
}else if(i<=400) {
System.out.println("你输入的用户用电量为"+i*0.45+"元");
}else
System.out.println("你输入的用户用电量为"+i*0.52+"元");
}
}
XZK-JAVA-支线任务-010103-002- 流程控制的逻辑训练任务(分支+循环综合)
1、计算应缴金额
商场根据会员积分打折:
2000 分以内打 9 折,
4000 分以内打 8 折,
8000 分以内打 7.5 折,
8000 分以上打 7 折, 使用 if-else-if 结构,实现手动输入购物金额和积分,计算出应缴金额
public static void main(String[] args) {
System.out.println("请输入会员积分:");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if(i<0) {
System.out.println("您输入的积分有误");
}else if(i<2000) {
System.out.println("应缴金额为"+i*0.90+"元");
}else if(i<4000) {
System.out.println("应缴金额为"+i*0.80+"元");
}else if(i<8000) {
System.out.println("应缴金额为"+i*0.75+"元");
}else {
System.out.println("应缴金额为"+i*0.70+"元");
}
}
}
2、计算该年该月天数
一年中有 12 个月,而每个月的天数是不一样的。其中大月 31 天,分别为1,3,5,7,8,10,12 月,小月 30 天,分别 为 4,6,9,11 月。还有二月比较特殊,平年的二月只有 28 天,而闰年的二月有 29 天,由用户在控制台输入年月日,程序计算输入的日期是当年的第多少天。 (例如输入 2000 年 12 月 31 日,应该输出是第 366 天)。
public static void main(String[] args) {
System.out.println("请输入年份和月份");
Scanner input = new Scanner(System.in);
int year = input.nextInt();
int mouth = input.nextInt();
switch(mouth){
case 1:case 3: case 5: case 7: case 8: case 10: case 12:
System.out.println("本月有31天");
break;
case 4: case 6 :case 9 :case 11:
System.out.println("本月有30天");
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
System.out.println("本月有29天");
}
else{
System.out.println("本月有28天");
}
break;
}
}
}
3、图形打印任务
在控制台中,编写三个 Demo,分别输出如下图形:
public static void main(String[]args) {
for(int i =0;i<5;i++) {
for(int j =0;j<i+1;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
public static void main(String[] args) {
for(int i =0;i<5;i++) {
for(int j =0;j<5-i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
public static void main(String[] args) {
for(int i =0;i<5;i++) {
for(int j=0;j<4-i;j++) {
System.out.print(" ");
}
for(int j=0;j<i*2+1;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
4、打印九九乘法表 ,效果如图:
public static void main(String[] args) {
System.out.println("九九乘法表");
for(int i=0;i<10;i++) {
for(int j=1;j<i+1;j++) {
System.out.print(j+"*"+i+"="+j*i+" ");
}
System.out.println();
}
}
}
5、打印三位数中的所有水仙花数
所谓“水仙花数”即一个整数满足其值等于各个数位的立方和。
如: 153 是一个水仙花数,因为 153= 1³+5³+3³
public static void main(String[] args) {
for(int i=100;i<1000;i++) {
int ge =i%10;
int shi=i/10%10;
int bai=i/100%10;
if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i) {
System.out.println(i);
}
}
}
}
人工智能的概念刚兴起时,网上流传了一段价值一个亿的代码,如下图