JAVASE--NO.5day(为期三个月的成长过程)
顺序结构
1.顺序结构按照代码书写的顺序一行一行执行。
public class Test {
public static void main(String[] args) {
System.out.println("aaa");
System.out.println("bbb");
System.out.println("ccc");
//如果调整代码的书写顺序, 则执行顺序也发生变化
//运行结果
// aaa
// bbb
// ccc
System.out.println("aaa");
System.out.println("ccc");
System.out.println("bbb");
//运行结果
// aaa
// ccc
// bbb
}
上来就是代码 没关系继续往下看 :
2.分支结构
2.1if 语句
语法格式:
语法格式1
if ( 布尔表达式 ){// 语句 1} else {// 语句 2}
比如:小明,如果这次考到90分以上,给你奖励一个大鸡腿,否则奖你一个大嘴巴子。
public class Test {
public static void main(String[] args) {
int score = 92;
if(score >= 90){
System.out.println("吃个大鸡腿!!!");
}else{
System.out.println("挨大嘴巴子!!!");
}
}
语法格式2
if ( 布尔表达式 ){// 语句}
如果布尔表达式结果为true,执行if中的语句,否则不执行。
public class Test {
//比如:小明,如果这次考试考到90分或以上,给你奖励一个鸡腿。
public static void main(String[] args) {
int score = 92;
if(score >= 90){
System.out.println("吃个大鸡腿!!!");
}
}
语法格式3
if ( 布尔表达式 1 ){// 语句 1} else if ( 布尔表达式 2 ){// 语句 2} else {// 语句 3}
表达式1成立,执行语句1,否则表达式2成立,执行语句2,否则执行语句。
public class Test {
public static void main(String[] args) {
int score = 90;//设置变量为90 用于参考
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80 && score < 90) {
System.out.println("良好");
} else if (score >= 70 && score < 80) {
System.out.println("中等");
} else if (score >= 60 && score < 70) {
System.out.println("及格");
} else if (score >= 0 && score < 60) {
System.out.println("不及格");
} else {
System.out.println("错误数据");
}
}
}
经典练习题:判断一个年份是否为闰年!!!
public class Test {
public static void main(String[] args) {
for (int year = 1500; year <= 2500 ; year++) {
//这里写的循环时1500年到25000年之间判断闰年
if (year % 100 == 0) {
// 判定世纪闰年
if (year % 400 == 0) {
System.out.println(year+"是世纪闰年");
} else {
System.out.println(year+"不是闰年");
}
} else {
// 普通闰年
if (year % 4 == 0) {
System.out.println(year+"是闰年");
} else {
System.out.println(year+"不是闰年");
}
}
}
}
重点: for (int year = 1500; year <= 2500 ; year++)快捷键 fori
这么良心的博主你还不点赞关注 O.o ???
2.2switch 语句
基本语法:
switch ( 表达式 ){case 常量值 1 :{语句 1 ;[ break ;]}case 常量值 2 :{语句 2 ;[ break ;]}default :{内容都不满足时执行语句 ;[ break ;]}}
- 先计算表达式的值
- 和case依次比较,一旦有响应的匹配就执行该项下的语句,直到遇到break时结束
- 当表达式的值没有与所列项匹配时,执行default
代码示例: 根据 day 的值输出星期
public class Test {
public static void main(String[] args) {
int day = 1;
switch(day) {
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;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("输入有误");
break;
}
}
//输出星期一
这用的就是Switch语句
- 多个case后的常量值不可以重复
- switch的括号内只能是以下类型的表达式:
- 基本类型:byte、char、short、int,注意不能是long类型
- 引用类型:String常量串、枚举类型
3.循环结构
3.1while 循环
基本语法格式:
while ( 循环条件 ){循环语句 ;}
public class Test {
public static void main(String[] args) {
int n = 1;
int result = 0;
while (n <= 100) {
result += n;
n++;
}
System.out.println(result);
}
//打印5050
思路:
解题思路:思路就是先去创建变量n让其在小于等于时100不断n++(n++ --> n = n + 1)然后创建result(结果)变量去存放数值“记住要为0,否则会以原有的数值上相加”然后让在while循环里不断+= n;直到n = 100;最后跳出程序那么算出来的数值就存放在result里,最后跳出循环然后打印它。
1. 和 if 类似 , while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句 . 建议还是加上 { }2. 和 if 类似 , while 后面的 { 建议和 while 写在同一行 .3. 和 if 类似 , while 后面不要多写 分号 , 否则可能导致循环不能正确执行 .
3.2break
break 的功能是让循环提前结束.
public static void main(String[] args) {
int num = 100;
while (num <= 200) {
if (num % 3 == 0) {
System.out.println("找到了 3 的倍数, 为:" + num);
break;
}
num++;
}
}
//输出就是 找到了 3 的倍数, 为:102
3.3continue
continue 的功能是跳过这次循环, 立即进入下次循环
代码示例: 找到 100 - 200 中所有 3 的倍数
public class Test {
public static void main(String[] args) {
int num = 100;
while (num <= 200) {
if (num % 3 != 0) {
num++; // 这里的 ++ 不要忘记! 否则会死循环.
continue;
}
System.out.println("找到了 3 的倍数, 为:" + num);
num++;
}
}
3.4for 循环
【基本语法】
for ( 表达式① ; 布尔表达式② ; 表达式③ ){表达式④ ;}
- 表达式1: 用于初始化循环变量初始值设置,在循环最开始时执行,且只执行一次
- 表达式2: 循环条件,满则循环继续,否则循环结束
- 表达式3: 循环变量更新方式
练习:计算 1! + 2! + 3! + 4! + 5!
public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
int tmp = 1;
for (int j = 1; j <= i; j++) {
tmp *= j;
}
sum += tmp;
}
System.out.println("sum = " + sum);
//sum = 153
}
【 注意事项 】 ( 和 while 循环类似 )
- 和 if 类似, for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
- 和 if 类似, for 后面的 { 建议和 while 写在同一行.
- 和 if 类似, for 后面不要多写 分号, 否则可能导致循环不能正确执行.
- 和while循环一样,结束单趟循环用continue,结束整个循环用break
3.5do while 循环
【基本语法】
do {循环语句 ;} while ( 循环条件 );
public static void main(String[] args) {
int num = 1;
do {
System.out.println(num);
num++;
} while (num <= 10);
}
- do while 循环最后的分号不要忘记
- 一般 do while 很少用到, 更推荐使用 for 和 while.
4.输入输出
4.1输出到控制台
重点!!!
基本语法
System . out . println ( msg ); // 输出一个字符串 , 带换行System . out . print ( msg ); // 输出一个字符串 , 不带换行System . out . printf ( format , msg ); // 格式化输出
- println 输出的内容自带 \n, print 不带 \n
- printf 的格式化输出方式和 C 语言的 printf 是基本一致的.
public class Test {
public static void main(String[] args) {
System.out.println("hello world");
int x = 10;
System.out.printf("x = %d\n", x);
}
JAVA格式化字符串
4.2从键盘输入
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.nextLine();
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
System.out.println("请输入你的工资:");
float salary = sc.nextFloat();
System.out.println("你的信息如下:");
System.out.println("姓名: " + name + "\n" + "年龄:" + age + "\n" + "工资:" + salary);
sc.close(); // 注意, 要记得调用关闭方法
}
}
5.猜数字游戏
代码实现:思路就是循环,先去建立一个0到100的数字引用 Random 去创建,然后使用学到的循环 While 先让数子随机编译好去存放在scanner(可以理解为就是一个容积)里,使用If循环让玩家知道自己猜大了还是小了。如果对了就跳出循环使用Break直接结束循环就可以了。
(相对于C语言的猜数字游戏JAVA的更好表达也更好理解)
C语言的猜数字游戏详解看我之前的博客:https://blog.csdn.net/Qwe_Jjh/article/details/126897685?spm=1001.2014.3001.5501
import java.util.Random;
import java.util.Scanner;
//这里是调用Random以及Scanner,否则程序会报错!!
//猜数字游戏的实现
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int randNum = random.nextInt(5 ) + 5;//[5~10)
//你也可以写[0~101) int randNum = random.nextInt(101);
//50-100的随机数 random.nextInt(50)--[0,50) + 50;
while (true) {
System.out.println("请输入你要猜的数字:");
int num = scanner.nextInt();
if(num < randNum) {
System.out.println("猜小了!");
}else if(num == randNum) {
System.out.println("猜对了!");
break;
}else {
System.out.println("猜大了!");
}
}
}
}
Scanner详情
Random详情
好啦结束啦,祝你有美好的一天