运算符和分支结构
1. 运算符
1.1 关系运算符
> < >= <= == !=
关系运算符的结果有且只有 true or false
用于在开发中的条件判断使用
1.2 逻辑运算符
&&
与,并且
同真为真,有假【即】假
||
或,或者
有真【即】真,同假为假
!
非,取反
强牛 犟,假变真来,真变假
逻辑运算符:
一般用于组合条件的判断,条件过滤,多情况分析。
断路原则
1.3 自增自减运算符
【注意事项】
1. 自增自减运算符有且只能操作变量!!!
2. 自增自减运算符独立存在!!!单独成行,减少代码中的歧义!!!
3. 【个人建议】
使用 += 1 和 -= 1 替代自增自减运算符
Swift取消了 自增自减运算符
++:
在变量之后,首先执行变量所在行语句,再执行自增1操作
在变量之前,首先执行变量自增1操作,再执行所在行语句
--:
在变量之后,首先执行变量所在行语句,再执行自减1操作
在变量之前,首先执行变量自减1操作,再执行所在行语句
【补充知识点 逻辑运算符断路原则问题】
class Demo4 {
public static void main ( String[ ] args) {
int num = 10 ;
boolean ret = 10 > 15 && num++ >= 10 ;
System. out. println ( "num : " + num) ;
System. out. println ( "ret : " + ret) ;
int num1 = 10 ;
boolean ret1 = 10 > 5 || ++ num1 > 5 ;
System. out. println ( "num1 : " + num1) ;
System. out. println ( "ret1 : " + ret1) ;
}
}
2. 从键盘上获取用户录入的数据[CV大法]
1. 导包,点技能点
import java.util.Scanner;
class Demo1 {}
2. 获取Scanner的变量, 扫描器的一个变量
Scanner sc = new Scanner(System.in);
3. 使用Scanner其中的方法
获取int类型数据 sc.nextInt();
获取double类型数据 sc.nextDouble();
获取String类型字符串数据 sc.next(); sc.nextLine();
获取一个char类型字符数据 sc.next().charAt(0);
sc.nextLine().charAt(0);
3. 分支结构
3.1 if分支结构
if ( ) {
}
3.2 if - else分支结构
if ( ) {
} else {
}
3.3 if - else if分支结构
if ( ) {
} else if ( ) {
} else if ( ) {
} else {
}
【补充知识点 用于输入数据的合法性判断问题】
在实际开发中,存在生活中数据的合法性和程序运行的语法要求冲突问题。
在程序的运行过程中,我们需要用户输入的数据进行合法性判断,如果用户输入的数据不是合法范围以内的,需要给予用户提示,并且不可以进入正常的代码运行过程中。
学生的成绩 0 ~ 100, 超出0~100都是不合法的!!!
1. 不能大于100 score > 100
2. 不能小于0 score < 0
3. 满足任何一个条件都是非法情况!!!
score > 100 || score < 0
4. if 判断
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 < 0 || score > 100 ) {
System. out. println ( "亲,您输入的数据不对哦~~~秋咪~~~" ) ;
System. exit ( 0 ) ;
}
if ( score >= 90 ) {
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 ( "让你爹穿着拖鞋来学校一趟!!!" ) ;
}
}
}
3.4 switch - case结构
switch ( ) {
case 值1 :
break ;
case 值2 :
break ;
default :
break ;
}
3.5 条件运算符/三目运算符
格式:
条件判断 ? true处理方式 : false处理方式;
条件运算符/三目运算符
使用局限性较大,类似于if - else结构。
3.6 分支结构嵌套
class Demo11 {
public static void main ( String[ ] args) {
int score = 89 ;
if ( score >= 90 ) {
if ( score >= 95 ) {
System. out. println ( "A+" ) ;
} else {
System. out. println ( "A-" ) ;
}
} else {
System. out. println ( "A--" ) ;
}
}
}
4. 局部变量
存在于代码块中的变量!!!
1. 局部变量有且只能在当前所处的代码块中使用,超出当前代码块范围无效!!!【作用域问题】
2. 在作用域范围以内,任何的地方可以直接使用对应的变量
3. 在一个变量的作用域范围以内,不能重定义同名变量,报错!
4. 局部变量未赋值不能使用
5. 练习
1. 完成条件:判断是否是闰年
能被4整除,但是不能被100整除,或者可以被400整除
a. year % 4 == 0
b. year % 100 != 0
c. year % 400 == 0
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
2. 完成条件:判断一个字符是不是大写英文字符
A ~ Z
'A' <= ch <= 'Z' 错误的!!!
ch >= 'A' && ch <= 'Z'
3. 完成条件:判断一个字符是不是英文字符
a. 英文字符中大写靠前,小写靠后
b. 大写和小写英文字符之间有其他字符
(ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
4. 使用if判断完成对应90后00后判断,用户输入年龄,判断情况。
5. 用户输入一个数值,判断是不是偶数
6. 用户输入自己的工资,判断是否需要缴税
class HomeWork1 {
public static void main ( String[ ] args) {
int age = 0 ;
if ( age >= 11 && age <= 20 ) {
System. out. println ( "00后" ) ;
} else if ( age >= 21 && age <= 30 ) {
System. out. println ( "90后" ) ;
} else {
System. out. println ( "老的算80后,小的算10后" ) ;
}
}
}
class HomeWork2 {
public static void main ( String[ ] args) {
int num = 0 ;
if ( num % 2 == 0 ) {
System. out. println ( "偶数" ) ;
} else {
System. out. println ( "奇数" ) ;
}
}
}
class HomeWork3 {
public static void main ( String[ ] args) {
double salary = 0.0 ;
if ( salary > 5000 ) {
System. out. println ( "我为国家做贡献,武汉加油,中国加油" ) ;
} else {
System. out. println ( "在我们GP班不要出现这种情况" ) ;
}
}
}
void main ( String[ ] args) {
double salary = 0.0 ;
if ( salary > 5000 ) {
System. out. println ( "我为国家做贡献,武汉加油,中国加油" ) ;
} else {
System. out. println ( "在我们GP班不要出现这种情况" ) ;
}
}
}