人工智障任务+逻辑训练
人工智障代码图
1、计算应缴金额 商场根据会员积分打折:
2000 分以内打 9 折
4000 分以内打 8 折
8000 分以内打 7.5 折
8000 分以上打 7 折,使用 if-else-if 结构,实现手动输入购物金额和积分, 计算出应缴金额
import java.util.Scanner;
public class discount {
public static void main(String[] args) {
Scanner jifen=new Scanner(System.in);
System.out.println(“输入积分:”);
int a=jifen.nextInt();
Scanner money=new Scanner(System.in);
System.out.println(“请输入金额”);
double n=money.nextInt();
if(0<a && a<=2000) {
n=n0.9;
}else if(2000<a && a<=4000) {
n=n0.8;
}else if(4000<a && a<=8000){
n=n0.75;
}else if(8000<a) {
n=n0.7;
}
System.out.println(“打折后为”+n);
}
}
2、计算该年该月天数 一年中有 12 个月,而每个月的天数是不一样的。其中大月 31 天,分别为 1,3,5,7,8,10,12 月,小月 30 天,分别 为 4,6,9,11 月。还有二月比较特殊,平 年的二月只有 28 天,而闰年的二月有 29 天,由用户在控制台输入年份和月份, 程序计算该年该月的天数。
import java.util.Scanner;
public class day {
public static void main(String[] args) {
Scanner year=new Scanner(System.in);
System.out.println(“请输入年份”);
Scanner month=new Scanner(System.in);
System.out.println(“请输入月份”);
int y=year.nextInt();
int m=month.nextInt();
int d=0;
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:d=31;
break;
case 4:
case 6:
case 8:
case 10:
case 12:d=30;
break;
case 2:
if((y%40&&y%100!=0)||y%4000){
d=28;
}else {
d=29;
} break;
}
System.out.println(d);
}
}
3、图形打印任务 在控制台中,编写三个 Demo,分别输出如下图形:
public class picture1 {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
for(int k=0;k<i+1;k++) {
System.out.print("*");
}
System.out.println();
}
}
}
public class picture2 {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
for(int k=5;k>i;k–) {
System.out.print("*");
}
System.out.println();
}
}
}
public class picture3 {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
for(int k=i+1;k<5;k++) {
System.out.print(" “);
}
for(int o=0;o<2i+1;o++) {
System.out.print("”);
}
System.out.println();
}
}
}
4、打印九九乘法表
package src;
public class chengfabiao {
public static void main(String[] args) {
for(int i=1;i<=9;i++) {
for(int k=1;k<=i;k++) {
System.out.print(i+""+k+"="+ik+"\t");
}
System.out.println();
}
}
}
5、打印三位数中的所有水仙花数 所谓“水仙花数”即一个整数满足其值等于各个数位的立方和。 如: 153 是一个水仙花数,因为 153= 1³+5³+3³
public class shuixianhua {
public static void main(String[] args) {
hua();
}
public static void hua() {
int a=0;
int b=0;
int c=0;
for(int i=100;i<1000;i++) {
a=i/100;
b=(i%100)/10;
c=(i%100)%10;
if(aaa+bbb+ccc==i) {
System.out.print(i+" ");
}
}
}
}