1. 分支结构
1.1 生活中的分支结构
大量的使用在
条件判断,等级判断中
1.2 开发中的分支结构
1.2.1 if 分支结构
if ( ) {
}
class Demo6 {
public static void main ( String[ ] args) {
boolean ret = false ;
if ( ret) {
System. out. println ( "中(河南话)" ) ;
}
System. out. println ( "请讲普通话" ) ;
}
}
1.2.2 if else 分支结构
if ( ) {
} else {
}
class Demo7 {
public static void main ( String[ ] args) {
boolean ret = false ;
if ( ret) {
System. out. println ( "中" ) ;
} else {
System. out. println ( "不中" ) ;
}
}
}
1.2.3 if else if 分支结构
if ( ) {
} else if ( ) {
} else if ( ) {
} else {
}
class Demo8 {
public static void main ( String[ ] args) {
int score = 59 ;
if ( score >= 90 && score <= 100 ) {
System. out. println ( "秀儿" ) ;
} else if ( score >= 80 ) {
System. out. println ( "良儿" ) ;
} else if ( score >= 70 ) {
System. out. println ( "中儿" ) ;
} else if ( score >= 60 ) {
System. out. println ( "过儿" ) ;
} else {
System. out. println ( "让你爹穿着拖鞋来学校" ) ;
}
}
}
【补充知识点 Scanner使用 CV大法/直接不会】
1. 点技能点
导包 在class 之前 复制粘贴
import java. util. Scanner;
2. 创建Scanner变量
固定格式:
Scanner sc = new Scanner ( System. in) ;
3. 通过Scanner变量,调用方法获取用户输入数据
需要获取int 类型
int num = 0 ;
num = sc. nextInt ( ) ;
需要获取float 类型
float num = 0.0F ;
num = sc. nextFloat ( ) ;
需要获取char 类型
char ch;
ch = sc. nextLine ( ) . charAt ( 0 ) ;
public static void main ( String[ ] args) {
double sum = 0 ;
int a = 0 ;
Scanner scanner = new Scanner ( System. in) ;
while ( scanner. hasNextDouble ( ) ) {
double v = scanner. nextDouble ( ) ;
a += 1 ;
sum = sum + v;
}
System. out. println ( a) ;
System. out. println ( sum) ;
scanner. close ( ) ;
}
【补充知识点 用户友好性提示】
在开发中,我们的产品需要考虑用户体验,增强用户粘性!!!
【补充知识点 用户输入数据合法性判断】
用户输入数据有可能出现输入错误,超出范围,不合理的情况!!!
加入合法性判断,如果数据不再合理范围以内,退出程序。
import java. util. Scanner;
class Demo8 {
public static void main ( String[ ] args) {
int score = 0 ;
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入学生的成绩:" ) ;
score = sc. nextInt ( ) ;
if ( score > 100 || score < 0 ) {
System. out. println ( "亲,学生成绩输入错误哦~~~啾咪(づ ̄3 ̄)づ╭?~" ) ;
System. exit ( 0 ) ;
}
if ( score >= 90 && score <= 100 ) {
System. out. println ( "秀儿" ) ;
} else if ( score >= 80 ) {
System. out. println ( "良儿" ) ;
} else if ( score >= 70 ) {
System. out. println ( "中儿" ) ;
} else if ( score >= 60 ) {
System. out. println ( "过儿" ) ;
} else {
System. out. println ( "让你爹穿着拖鞋来学校" ) ;
}
}
}
1.3 switch - case 分支结构
switch ( ) {
case 常量1 :
处理方式1 ;
break ;
case 常量2 :
处理方式2 ;
break ;
case 常量3 :
处理方式3 ;
break ;
default :
最终处理方式;
break ;
}
import java. util. Scanner;
class Demo1 {
public static void main ( String[ ] args) {
int choose = 0 ;
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "1. 枸杞 100/KG" ) ;
System. out. println ( "2. 陈皮 188/0.25KG" ) ;
System. out. println ( "3. 菊花 15/KG" ) ;
System. out. println ( "4. 蒲公英 200/KG" ) ;
System. out. println ( "请选择您要购买的药物:" ) ;
choose = sc. nextInt ( ) ;
switch ( choose) {
case 1 :
System. out. println ( "人到中年不得已,保温杯里泡枸杞" ) ;
break ;
case 2 :
System. out. println ( "陈皮 糖~~~" ) ;
break ;
case 3 :
System. out. println ( "菊花 茶~~~" ) ;
break ;
case 4 :
System. out. println ( "蒲公英 " ) ;
break ;
default :
System. out. println ( "请输入正确的选项" ) ;
break ;
}
}
}
1.4 switch case 使用注意事项
1. 在 switch case结构中,能够执行的代码,有且只能是case之后,或者default之后的内容
2. switch case结构中,不允许出现相同的case选择
3. case之后可以省略break,当前case选项执行过程中,需要明确的找到下一个break,跳出switch case结构
4. 允许多个case并列出现,同时执行同一个操作过程。
5. default可以省略,但是基于开发原则,不建议省略default操作。
2. 循环结构
2.1 为什么要用循环
代码中一定会出现,大量重复功能的代码模块,如果使用CV大法会导致以下一些问题:
1. 代码冗余,过于臃肿
2. 阅读性极差
3. 维护性极差
2.2 while循环结构
while ( ) {
}
class Demo3 {
public static void main ( String[ ] args) {
int num = 10 ;
while ( num > 0 ) {
System. out. println ( "中午吃热干面~~~" ) ;
num -= 1 ;
}
}
}
2.3 do while循环结构
do {
} while ( ) ;
class Demo4 {
public static void main ( String[ ] args) {
int num = 10 ;
do {
System. out. println ( "请讲普通话" ) ;
num -= 1 ;
} while ( num > 0 ) ;
}
}
2.4 for循环结构【重点,重点,重点】
for ( ; ; ) {
}
public class test {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 9 ; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
System. out. print ( i+ "*" + j+ "=" + ( i* j) + "\t" ) ;
}
System. out. println ( ) ;
}
}
}
2.5 break和continue区别
break:直接跳出循环,终止循环
continue:跳出本次循环,继续下一次循环,即判断是否开始下一次循环,回到循环最开始的地方
public class TestBreak {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 100 ; i++ ) {
if ( i % 10 == 0 ) {
break ;
}
System. out. print ( i+ "\t" ) ;
}
}
}
public class TestContinue {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 100 ; i++ ) {
if ( i % 10 == 0 ) {
continue ;
}
if ( i% 5 == 0 ) {
System. out. println ( ) ;
}
System. out. print ( i+ " " ) ;
}
}
}
2.6 for循环练习
public class Test {
public static void main ( String[ ] args) {
for ( int i = 0 ; i <= 5 ; i++ ) {
for ( int j = 5 ; j >= i; j-- ) {
System. out. print ( " " ) ;
}
for ( int j = 1 ; j <= 2 * i- 1 ; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
}