1、Swtich
import java.util.Scanner;
/*
春夏秋冬
需求:
一年有12个月,分属于春夏秋冬4个季节,键盘录入一个月份,请用程序实现判断该月份属于哪个季节,并输出。
春:3、4、5。夏:6、7、8。秋:9、10、11。冬:1、2、12
*/
public class test {//类
public static void main(String[] args) {//主方法,调用
Scanner sc=new Scanner(System.in);//键盘录入月份数据,使用变量接受
System.out.println("请输入一个月份:");//输出提示语句
int month= sc.nextInt();//变量接受数据
//多情况判断,这里采用switch语句实现
//在每种情况中,完成输出对应的季节
switch(month){//表达式:接受上面的变量 得到数字后与case的值比较,生成对应的结果
case 1:
case 2:
case 12://case穿透
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;
}
}
}
2、for语句
/*
水仙花数:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。
需求:
在控制台输出所有的"水仙花数"
*/
public class text {
public static void main(String[] args) {
//输出所有的水仙花数必然要使用到循环,通历所有的三位数,三位数从1oo开始,到ss结束
for (int i=100;i<1000;i++){//初始化语句;条件判断语句;条件控制语句
//循环体语句
//在计算之前获取三位数中每个位上的值
int ge=i%10;//取余
int shi=i/10%10;
int bai=i/10/10%10;
//判定条件是将三位数中的每个数值取出来,计算立方和后与原始数字比较是否相等
if (ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
//输出满足条件的数字就是水仙花数
System.out.println(i);
}
}
}
}