第三章 流程控制
3.1 程序输入输出
3.1.1 输入
java.util.Scanner
package cn.yuan;
import javax.swing.*;
import java.util.Scanner;
public class InputDemo {
public static void main(String[] args) {
//System.out.println("欢迎:"+args[0]);
//图像窗口输入
String n= JOptionPane.showInputDialog("请输入姓名");
System.out.println("欢迎:"+n);
//输入Scanner
Scanner scanner=new Scanner(System.in);
System.out.println("请输入姓名:");
String name=scanner.nextLine();
System.out.println("请输入年龄:");
int age= scanner.nextInt();
System.out.println();
System.out.printf("姓名:%s,年龄:%d岁%n",name,age);
}
}
运行结果:
欢迎:张三
请输入姓名:
张三
请输入年龄:
18
姓名:张三,年龄:18岁
3.1.2 输出
package cn.yuan;
import java.util.Random;
public class OutDemo {
public static void main(String[] args) {
//控制输出颜色 背景 前景
Random random = new Random();
for (int i = 1; i <= 55; i++) {
System.out.printf("\033[%d;%dm hello world \033[0m ", random.nextInt(30, 39), random.nextInt(40, 49));
if (i % 5 == 0) {
System.out.println();
}
}
//控制输出颜色 背景 前景
System.out.println("\033[30m hello 30 world \033[0m ");
System.out.println("\033[31m hello 31 world \033[0m ");
System.out.println("\033[32m hello 32 world \033[0m ");
System.out.println("\033[33m hello 33 world \033[0m ");
System.out.println("\033[34m hello 34 world \033[0m ");
System.out.println("\033[35m hello 35 world \033[0m ");
System.out.println("\033[36m hello 36 world \033[0m ");
System.out.println("\033[37m hello 37 world \033[0m ");
System.out.println("\033[38m hello 38 world \033[0m ");
//添加背景
System.out.println("\033[30;49m hello 30 world \033[0m ");
System.out.println("\033[41m hello 30 world \033[0m ");
System.out.println("\033[42m hello 31 world \033[0m ");
System.out.println("\033[43m hello 32 world \033[0m ");
System.out.println("\033[44m hello 33 world \033[0m ");
System.out.println("\033[45m hello 34 world \033[0m ");
System.out.println("\033[46m hello 35 world \033[0m ");
System.out.println("\033[47m hello 36 world \033[0m ");
System.out.println("\033[48m hello 37 world \033[0m ");
}
}
3.2 分支语句
3.2.1 if else if else
package cn.yuan;
import java.util.Calendar;
import java.util.Scanner;
public class If1 {
public static void main(String[] args) {
// 分支语句 if()语句
Calendar c=Calendar.getInstance();
System.out.printf("%tA%n",c);
//判断星期
int a=c.get(Calendar.DAY_OF_WEEK);//1 周日
System.out.println(a);
if (a==1){
System.out.println("周日");
} else if (a==2) {
System.out.println("周一");
}else if (a==3) {
System.out.println("周二");
}else if (a==4) {
System.out.println("周三");
}else if (a==5) {
System.out.println("周四");
}else if (a==6) {
System.out.println("周五");
}else {
System.out.println("周六");
}
//判断月份
int b=c.get(Calendar.MONTH);
System.out.println(b);
if (b==0){
System.out.println("一月");
} else if (b==1) {
System.out.println("二月");
}else if (b==2) {
System.out.println("三月");
}else if (b==3) {
System.out.println("四月");
}else if (b==4) {
System.out.println("五月");
}else if (b==5) {
System.out.println("六月");
}else if (b==6) {
System.out.println("七月");
}else if (b==7) {
System.out.println("八月");
}else if (b==8) {
System.out.println("九月");
}else if (b==9) {
System.out.println("十月");
}else if (b==10) {
System.out.println("十一月");
}else {
System.out.println("十二月");
}
//判断闰年
Scanner scanner=new Scanner(System.in);
System.out.println("请输入年份:");
int d= scanner.nextInt();
if (d%400==0 || (d%4==0 &&d%100!=0)){
System.out.println("这一年是闰年");
}else {
System.out.println("这一年不是闰年");
}
}
}
运行结果:
星期一
2
周一
1
二月
请输入年份:
2000
这一年是闰年
package cn.yuan;
import java.util.Calendar;
public class If2 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int a = c.get(Calendar.HOUR_OF_DAY);
String s = a < 12 ? "上午" : "下午";
System.out.println(s);
}
}
运行结果:
下午
3.2.2 switch case break default
package cn.yuan;
import java.util.Scanner;
public class Switch1 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println(" 请输入年份:");
int year=scanner.nextInt();
System.out.println("请输入月份:");
int month=scanner.nextInt();
int days;
/* int year=2023;
int month=2;
int days=31;*/
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
default:
days=year%400==0 || year%100!=0&& year%4==0 ? 29:28;
break;
}
System.out.printf("%d年%d月有%d天%n",year,month,days);
}
}
运行结果:
请输入年份:
2000
请输入月份:
3
2000年3月有31天
//计算前一天星期几?
package cn.yuan;
import java.util.Calendar;
public class Switch2 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);//昨天是星期几
int n = c.get(Calendar.DAY_OF_WEEK);//这两句一个意思
//int n = c.get(7);
switch (n) {
case 1:
System.out.println("星期日");
break;
case 2:
System.out.println("星期一");
break;
case 3:
System.out.println("星期二");
break;
case 4:
System.out.println("星期三");
break;
case 5:
System.out.println("星期四");
break;
case 6:
System.out.println("星期五");
break;
default:
System.out.println("星期六");
break;
}
}
}
运行结果:
星期日
switch语句面试题,switch语句支持哪些类型?
char(Character),int(Integer),byte(Byte),short(Short),String,enum这几种类型
3.3 循环语句
3.3.1 for/for each
package cn.yuan;
public class For1 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
System.out.printf("%d + %d=%d%n", sum, i, sum += i);
}
System.out.println(sum);
for (int i = 1; i <= 5; i++) {
System.out.printf("%d ", i);
}
System.out.println();
for (int i = 5; i >= 1; i--) {
System.out.printf("%d ", i);
}
}
}
运行结果:
......前面省略
4371 + 94=4465
4465 + 95=4560
4560 + 96=4656
4656 + 97=4753
4753 + 98=4851
4851 + 99=4950
4950 + 100=5050
5050
1 2 3 4 5
5 4 3 2 1
package cn.yuan;
//九九乘法表
public class For3 {
public static void main(String[] args) {
for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= a; b++) {
System.out.printf("%8d *%2d = %2d", a, b, a * b);
}
System.out.println();
}
}
}
运行结果:
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
package cn.yuan;
public class For4 {
public static void main(String[] args) {
//生成菱形
int s = 5;
for (int i = 1; i < 6; i += 1) {
s--;
for (int t = s; t >= 1; t--) {
System.out.print(" ");
}
for (int n = 1; n <= i; n++) {
System.out.print(" *");
}
System.out.println();
}
for (int i = 4; i >= 1; i -= 1) {
s++;
for (int t = s; t >= 1; t--) {
System.out.print(" ");
}
for (int n = 1; n <= i; n++) {
System.out.print(" *");
}
System.out.println();
}
}
}
运行结果:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
package cn.yuan;
//增强for循环
import java.util.List;
import java.util.Set;
public class ForEach {
public static void main(String[] args) {
//声明int[]数组
//遍历数组
int[] nums = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
System.out.println();
//逆序遍历
for (int i = nums.length - 1; i >= 0; i--) {
System.out.print(nums[i] + " ");
}
System.out.println();
//for each:遍历数组
for (int i : new int[]{1, 2, 3}) {
System.out.println(i);
}
//顺序
List<String> list = List.of("java", "html", "mysql");
for (var s : list
) {
System.out.println(s);
}
//逆序
for (int i = list.size() - 1; i >= 0; i--) {
System.out.println(list.get(i));
}
System.out.println(System.getProperty("jave.version"));
Set<Object> ks = System.getProperties().keySet();
for (Object k : ks) {
System.out.println(k + "=" + System.getProperty(k.toString()));
}
}
}
运行结果:
1 2 3 4 5 6
6 5 4 3 2 1
1
2
3
java
html
mysql
mysql
html
java
null
java.specification.version=17
sun.cpu.isalist=amd64
sun.jnu.encoding=GBK
package cn.yuan;
public class BirdDemo {
public static void main(String[] args) {
//百钱百鸡案例 鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何
int a, b, c;
for (a = 0; a <= 20; a++) {
for (b = 0; b <= 34; b++) {
for (c = 0; c <= 100; c++) {
if (c % 3 != 0)
continue;
int s1 = a + b + c;
int s2 = 5 * a + 3 * b + c / 3;
if (s1 == 100 && s2 == 100) {
System.out.printf("%d % d %d\n", a, b, c);
}
}
}
}
}
}
运行结果:
0 25 75
4 18 78
8 11 81
12 4 84
3.3.2 while
条件循环语句:
package cn.yuan;
public class While1 {
public static void main(String[] args) {
//1-5
int i = 1;
while (i <= 5) {
System.out.println(i++);
}
--i;
while (i >= 1) {
System.out.printf("%03d\t", i);
--i;
}
}
}
运行结果:
1
2
3
4
5
005 004 003 002 001
package cn.yuan;
public class DoWhile {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
//i++;
++i;
break;
} while (i <= 10);
}
}
运行结果:
1
package cn.yuan;
//猜数字游戏
import java.util.Random;
import java.util.Scanner;
public class GuessNum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int[] level = {100, 200, 500, 1000};
int le = level[random.nextInt(level.length)];
int a = random.nextInt(1, le + 1);
// int a = random.nextInt(1, 101);
int num = 0;//计数器,计算一个猜了对少次。
while (true) {
++num;
System.out.print("\033[34m请输入数字[1-" + le + "]:\033[0m");
int i = scanner.nextInt();
if (i > a) {
System.out.printf("\033[31m猜了%d次,你猜大了。\033[0m\n", num);
} else if (i < a) {
System.out.printf("\033[31m猜了%d次,你猜小了。\033[0m\n", num);
} else {
System.out.printf("\033[32m你一共猜了%d次才猜对了。\033[0m\n", num);
break;
}
}
System.out.printf("\033[36m游戏结束。你得了%d分。\033[0m", 100 - num);
}
}
运行结果:
请输入数字[1-200]:100
猜了1次,你猜小了。
请输入数字[1-200]:150
猜了2次,你猜大了。
请输入数字[1-200]:130
猜了3次,你猜大了。
请输入数字[1-200]:110
猜了4次,你猜小了。
请输入数字[1-200]:120
猜了5次,你猜大了。
请输入数字[1-200]:115
猜了6次,你猜小了。
请输入数字[1-200]:116
猜了7次,你猜小了。
请输入数字[1-200]:117
猜了8次,你猜小了。
请输入数字[1-200]:118
你一共猜了9次才猜对了。
游戏结束。你得了91分。
3.3.3 do while
package cn.yuan;
public class DoWhile {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
//i++;
++i;
break;
} while (i <= 10);
}
}
运行结果:
1
3.3.4 break continue label
break语句出现在 switch语句,循环语句,代表退出当前的程序执行块
continue 结束本次循环,立即开始下次循环
break、continue
package cn.yuan;
public class BreakDemo {
public static void main(String[] args) {
while (true) {
System.out.println(2);
//退出终止当前循环语句,执行到循环语句后
break;
}
for (int i = 0; i < 20; i++) {
if (i % 2 == 0) {
//结束本次,立即开始下一次循环
continue;
}
System.out.print(i + " ");
if (i > 10) {
break;
}
}
System.out.println();
}
}
运行结果:
2
1 3 5 7 9 11
label标签,主要出现在循环语句前,主要解决多层嵌套循环语句时,直接break 标签名; continue 标签;进行执行跳转,变相实现了goto语句的作用。
package cn.yuan;
public class BreakDemo {
public static void main(String[] args) {
//label标签,只能出现循环语前
ss:
for (int i = 1; i <= 15; i++) {
System.out.printf("%03d%n", i);
System.out.println("------------------------------");
while (Math.random() > 0.01) {
double d = Math.random();
System.out.println(d);
if (d > .8) {
break ss;
}
}
for (int a = 1; a <= 6; a++) {
System.out.println(a);
}
}
}
}
运行结果:
001
------------------------------
0.3407902877553255
0.07267680846420554
0.849266924714578
ring[] args) {
//label标签,只能出现循环语前
ss:
for (int i = 1; i <= 15; i++) {
System.out.printf(“%03d%n”, i);
System.out.println(“------------------------------”);
while (Math.random() > 0.01) {
double d = Math.random();
System.out.println(d);
if (d > .8) {
break ss;
}
}
for (int a = 1; a <= 6; a++) {
System.out.println(a);
}
}
}
}
运行结果:
001
0.3407902877553255
0.07267680846420554
0.849266924714578