这次是2021年01月11日的学习成果,总结于此,以便于之后的学习查漏补缺
顺序结构:没什么好说的,直接从上往下打就完事了!
选择结构:if 语句、switch语句
if语句
public static void main(String[] args) {
//判断产品等级,90~100为A,80到90为B,70到80为C,70及以下为D
int sorce = 99;
if(sorce <= 70) {
System.out.println("此产品为"+"D"+"级");
}else if(sorce <=80) {
//可以这么写,因为在数据输入后,会先执行第一条,
//满足条件后会直接输出,不会执行第二条,故不影响。
System.out.println("此产品为"+"C"+"级");
}else if(sorce > 80 && sorce <=90) {
System.out.println("此产品为"+"B"+"级");
}else {
System.out.println("此产品为"+"A"+"级");
}
System.out.println("--------------------------");
//判断输入的月份为哪个季节
int month = 3;
if (month >= 1 && month <= 12) {
if (month == 3 || month == 4 || month == 5) {
System.out.println("Spring");
} else if (month == 6 || month == 7 || month == 8) {
System.out.println("Summer");
} else if (month == 9 || month == 10 || month == 11) {
System.out.println("Autumn");
} else if (month == 12 || month == 1 || month == 2) {
System.out.println("Winter");
}
}else {
System.out.println("请输入正确的月份。");
}
}
}
switch语句:用于分支明确且较少的情况
public static void main(String[] args) {
//判断当前红绿灯状况
short color = 2;
switch(color) {
case 1:
System.out.println("红灯");
break;
case 2:
System.out.println("黄灯");
break;
case 3:
System.out.println("绿灯");
break;
default:
System.out.println("输入的数字有误");
break;
}
System.out.println("------------------------");
//两个整数的四则运算
int a = 5;
int b = 6;
char oper = '+';
switch(oper) {
case '+':
System.out.println(a+b);
break;
case '-':
System.out.println(a-b);
break;
case '*':
System.out.println(a*b);
break;
case '/':
System.out.println(a/b);
break;
default:
System.out.println("输入有误");
break;
}
System.out.println("-----------------------");
//判断今天是否为工作日/休息日
int day = 1;
switch(day) {
case 1:
case 2:
case 3:
case 4:
case 5:
//switch的穿透性
System.out.println("工作日");
break;
case 6:
case 7:
System.out.println("休息日");
break;
default:
System.out.println("输入有误");
break;
}
}
}
循环结构:for 、while 、 do…while
for循环:已知循环次数
//打印1到100内能整除3和整除5的数
for(int i=1;i<101;i++) {
if(i%3==0 && i%5==0) {
System.out.print(i+" ");
}
}
System.out.println();
System.out.println("----------------------");
//打印1到100内所有偶数的和
int evensum = 0;
for(int i=1;i<101;i++) {
if(i%2==0){
evensum = evensum + i;
System.out.println("此时i为:"+i);
System.out.println("此时偶数总和evensum为:"+evensum);
}
}
一些有意思的for循环
public static void main(String[] args) {
//打印直角三角形
//三角形总行数遍历
for(int i=1;i<=9;i++) {
//每行的 * 数量
for(int j=1;j<=i;j++) {
System.out.print("*");
}
System.out.println();
}
运行结果:
public static void main(String[] args) {
//打印等边三角形
//遍历三角形的总层数
for (int i = 1; i <= 5; i++) {
//打印每行的空格数
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
//打印每行的 * 数量
for(int k=1;k<=2*i-1;k++) {
System.out.print("*");
}
System.out.println();
}
}
}
运行结果:
public static void main(String[] args) {
//打印9*9乘法表
for(int i=1;i<=9;i++) {
for(int k=1;k<=i;k++) {
System.out.print(i+"*"+k+"="+(i*k)+" ");
}
System.out.println();
}
}
}
运行结果:
while循环:未知循环次数,满足条件才循环
//打印整数一到十
int i =1;
while(i<=10) {
System.out.print(i+" ");
i++;
}
System.out.println();
do…while:未知循环次数,无论是否满足条件,至少循环一次
//输出5次 起飞!!!
//do...while 起码能输出一次
int i = 1;
do {
System.out.println("起飞!!!");
i++;
}
while(i<6);
控制语句 break 、 continue
public static void main(String[] args) {
for(int i=1;i<51;i++) {
if(i == 33) {
//终止整个循环
break;
}if(i%2 !=0) {
//终止这一次,其余继续输出
continue;
}
System.out.println(i);
}
}
}
利用for循环打印水仙花数
//打印出100~1000范围内的所有 “水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
//例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
for(int i = 100; i < 1000; i++) {
int singleDigits = i % 10;
int tenDigits = (i / 10) % 10;
int hundredsDigits = i / 100;
int singleDigitsCubic=singleDigits*singleDigits*singleDigits;
int tenDigitsCubic=tenDigits*tenDigits*tenDigits;
int hundredsDigitsCubic=hundredsDigits*hundredsDigits*hundredsDigits;
int sum = singleDigitsCubic+tenDigitsCubic+hundredsDigitsCubic;
if(i==sum) {
System.out.println(i+"是水仙花数");
}
}