1.if语句(分支结构):
if(
布尔表达式
){
满⾜时执⾏语句
}else{
不满⾜
}
If~else
语句最好不要超过三层嵌套
If
()
{
Return ;
}
表示结束判断,不在向下执⾏操作!!!!
能⽤
if
()
{}
并列的,不要⽤
if
嵌套!!
卫语句:
把⾮法的操作,⽤
if
语句并列在前⾯,只要有异常操作就结束操作,然后把正常操作的逻辑写在下⾯
例⼦
:
异常语句:
If(false1){
Return ;
}
If(false2){
Return ;
}
.........
在写正常逻辑的时候不同情况的操作!!
If(ture){
System.out.println();
}else{
System.out.println();
}
switch
不能⽤布尔表达式,之前只能⽤整数,现在可以⽤于枚举和
string
语句;
switch
(整数
|
字符
|
枚举
|string
)
{case
:内容
break
; 。。。。。
default}
2.while语句
while 循环属于先判断条件,然后操作;do-while 判断,先执⾏⼀次,然后在操作;记住:以后的开发要
彻底忘记 do-while;
while
(
i
<=100) {
sum
+=
i
;
i
++;
}
System.
out
.println(
sum
);
Do while
循环:不⽤记;
3.for 循环
for
(
i
=1;
i
<=100;
i
++) {
sum
+=
i
;
}
System.
out
.println(
sum
);
for
(循环初始化条件 ;循环判断;循环变更条件)
{
循环语句;
}
for
(循环初始化条件 ;循环判断;循环变更条件)
{
循环语句;
}
For 的⽆限循环
for
(;;) {
System.
out
.println(
"你好"
);
}
While 的⽆限循环
While
(
true
)
{
System.
out
.println(
"你好"
);
}
注意:
如果不知道循环次数,但是知道循环结束条件的时候⽤
while
循环;例⼦:吸⼏根烟就死了。 如果知道循
环次数时候,使⽤
for
循环;例⼦:如果吸
100
根烟就死了。
// 范例:嵌套模拟吸烟:
int max=9;
for(int x=1;x<=max;x++) {
System.out.println("开始抽第"+ x +"根烟");
for(int y=1;y<=20;y++) {
System.out.print("嘬、");
}
System.out.println();
5.关于这部分的一些代码应用
(1)计算闰年的
if
语句:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输⼊⼀个年份:");
int y=sc.nextInt();
if(y%4==0) {
if(y%100!=0 || y%400==0) {
System.out.println("这是⼀个闰年!!");
}else {
System.out.println("这不是⼀个闰年!!");
}
}else {
System.out.println("这不是闰年!!");
}
}
(2)关于成绩的switch语句
public void text4() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score >= 100 || score <= 0) {
System.out.println("请重新输入");
return;
}
switch (score / 10) {
case 10:
case 9:
System.out.println("优秀");
break;
case 8:
System.out.println("良好");
break;
case 7:
System.out.println("一般");
break;
case 6:
System.out.println("及格");
break;
default:
System.out.println("不及格");
}
}
(3)输入月份、年份判断天数:
public void text5() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份");
int month = scanner.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
case 2:
System.out.println("请输入年份");
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("29天");
} else {
System.out.println("28天");
}
}
}
(4)打印正三角形和倒三角形
public void work2() {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 11; j++) {
if (j < 6 + i && j > 6 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 11; j++) {
if (j < 13 - i && j > i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
(5)随机生成一个整数。1-1000用户输入一个整数,程序给出与存储的数字是“大”或者“小”,知道用户猜到这个数字位置。如果中途用户希望程序退出,输入0可以退出。
public void test5() {
int random = new Random().nextInt(100) + 1;
while (true) {
System.out.println("请输⼊⼀个 0~100 之间的整数:");
Scanner scanner = new Scanner(System.in);
int userNum = scanner.nextInt();
if(userNum == 0) {
System.out.println("退出游戏!");
break;
}
if (random > userNum) {
System.out.println("猜⼩了!");
}else if (random < userNum) {
System.out.println("猜⼤了!");
}else {
System.out.println("恭喜你猜对了!");
break; //