JAVA part 2:运算符、Scanner、if和switch语句
运算符
对常量和变量进行操作的符号
表达式
用运算符吧常量或者变量连接起来符合JAVA语法的式子
不同类型运算符连接的狮子体现的是不同类型的表达式
举例
两个int类型的变量a,b,做加法(a+b)
常用运算符
1.算术运算符
" + " , " - " , " * " , " / " , " % "
package Day_2;
public class test1 {
public static void main(String[] args) {
int a = 20;
int b = 9;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
System.out.println(5/4.0);
}
}
tips:
①" / "是获取两个数据相除的商, " % "是获取两个数据相除的余数
②整数相除只能得到整数,想要得到小数,就必须有浮点数参与运算
// 字符和字符串参与加法运算
package Day_2;
public class test2 {
public static void main(String[] args) {
int a = 10;
char ch = 'a';
System.out.println(a+ch);
System.out.println(a+a+"hello");
//输出结果为20hello
System.out.println("hello"+a+a);
//输出结果为hello1010
}
}
tips:
字符串做加法,如果是10+10+“hello”,则运算顺序是先做数字的加法10+10,结果是20,然后(10+10)再和字符串"hello"做运算,结果是20hello;
如果是"hello"+10+10,则运算顺序是先做字符串运算"hello"+10,结果是hello10,然后hellow10再和数字10做运算,结果是hello1010
2.自增自减运算符
" ++ " , " - - "
package Day_2;
public class test3 {
public static void main(String[] args) {
int a = 10;
a++;
System.out.println("a:"+a);
// 输出结果为a:11
int b = 10;
b--;
System.out.println("b:"+b);
// 输出结果为b:9
int c = a++;
System.out.println("a:"+a);
// 输出结果为a:12
System.out.println("c:"+c);
// 输出结果为c:11
int d = ++b;
System.out.println("b:"+b);
// 输出结果为b:10
System.out.println("d:"+d);
// 输出结果为d:10
}
}
tips:
①++或- -的作用就是自己+1或自己-1
②++或- -可以在变量前面也可以在变量后面
③当单独使用的时候,++a和a++的效果是一样的
④如果有其它变量参与的时候:
++在前,先变量自增,后赋值操作
++在后,先赋值操作,再变量自增
3.赋值运算符
基本运算符 " = "
扩展运算符 " += " ," -= " , " *= " , " /= " , " %= "
package Day_2;
public class test4 {
public static void main(String[] args) {
short s = 1;
s = s + 2;
System.out.println("s:"+s);
输出报错
short s = 1;
s += 2;
System.out.println("s:"+s);
//输出结果为s:3
}
}
tips:
+=1 即为s = s + 1。
如果s是short等类型,在 s = s + 1 的操作时会自动将 s+1 的结果的数据类型由short转变成int,int类型的值没办法赋值给short类型,所以会产生报错。
而在使用 s += 1时,底层数据为 s =(s的数据类型)(s+1),这样就不存在数据类型报错。
4.关系运算符
" == " , " != " , " > " , " >= " , " < " , " <= "
package Day_2;
public class test5 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
System.out.println(a==b);
// 输出结果为false
System.out.println(a==c);
// 输出结果为true
System.out.println(a!=b);
// 输出结果为true
System.out.println(a!=c);
// 输出结果为false
System.out.println(a>b);
// 输出结果为false
System.out.println(a>c);
// 输出结果为false
System.out.println(a>=b);
// 输出结果为false
System.out.println(a>=c);
// 输出结果为true
System.out.println(a<b);
// 输出结果为ture
System.out.println(a<c);
// 输出结果为false
System.out.println(a=b);
// 输出结果为20
}
}
tips:
关系运算符的输出结果时boolean类型
a==b 输出结果是bollean类型,而 a=b 则是将b的值赋给a并输出a
5.逻辑运算符
" && " , " || " , " ! "
package Day_2;
public class test6 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
System.out.println((a>b)&&(a>c));// false && false
// 输出结果为 false
System.out.println((a<b)&&(a>c));// true && false
// 输出结果为 false
System.out.println((a<b)&&(a<c));// true && true
// 输出结果为 true
System.out.println((a>b)||(a>c));// false || false
// 输出结果为 false
System.out.println((a<b)||(a>c));// true || false
// 输出结果为 true
System.out.println((a<b)||(a<c));// true || true
// 输出结果为 true
System.out.println((a>b));
// 输出结果为false
System.out.println(!(a>b));
// 输出结果为true
System.out.println(!!(a>b));
// 输出结果为false
}
}
tips:
逻辑非 ! ,使用奇数个会改变boolean值,使用偶数个则不会改变boolean值
6.三元运算符
(关系表达式)? (表达式1) : (表达式2) ;
package Day_2;
public class test7 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = (a>b)?a:b;
System.out.println("c:"+c);
}
}
tips:
执行流程为:先计算关系表达式的值,看结果是true还是false;如果是true,则输出表达式1;如果是false,则输出表达式2
Scanner
为了提高程序灵活性,我们把数据改进为键盘录入,使用的就是Scanner
package Day_2;
import java.util.Scanner;
public class test8 {
public static void main(String[] args) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
// 给出提示
System.out.println("请输入一个整数:");
// 获取数据
int i = sc.nextInt();
//把获取的数据输出
System.out.println("i:"+i);
}
}
tips:
使用步骤:
①导包
import java.util.Scanner;
需要注意的是在一个类中,顺序关系是 package > import > class
②创建键盘录入对象
Scanner sc = new Scanner(System.in);
③获取数据
int i = sc.nextInt();
流程控制语句
结构顺序为:从上到下,依次执行
package Day_2;
public class test9 {
public static void main(String[] args) {
System.out.println("开始");
System.out.println("语句块A");
System.out.println("语句块B");
System.out.println("语句块C");
System.out.println("结束");
}
}
if语句
格式分为三种:
①格式一:
if(关系表达式){
语句块
}
package Day_2;
public class test10 {
public static void main(String[] args) {
System.out.println("开始");
int a = 10;
int b = 20;
int c = 10;
//判断两个变量是否相等
if(a==b){
System.out.println("a=b");
}
if (a==c){
System.out.println("a=c");
}
System.out.println("结束");
}
}
②格式二:
if(关系表达式){
语句块1;
}
else{
语句块2;
}
package Day_2;
public class test11 {
public static void main(String[] args) {
System.out.println("开始");
int a = 10;
int b = 20;
if(a==b){
System.out.println("a=b");
}
else{
System.out.println("a!=b");
}
System.out.println("结束");
}
}
③格式三:
if(关系表达式1){
语句块1;
}else if (关系表达式2){
语句块2;
}
......
else{
语句块n+1;
}
package Day_2;
public class test12 {
public static void main(String[] args) {
System.out.println("开始");
int a = 10;
int b = 20;
int c = 70;
if(a+b==c){
System.out.println("a+b=c");
}else if (a+b*2==c){
System.out.println("a+b*2=c");
}else if (a+b*3==c) {
System.out.println("a+b*3=c");
}else if (a+b*4==c) {
System.out.println("a+b*4=c");
}
else{
System.out.println("a+b永远不可能等于c");
}
}
}
switch语句
格式:
switch(表达式){
case值1:
语句块;
break;
case值2:
语句块2;
break;
......
default:
语句块n+1;
break;
}
执行流程:
①计算表达式的值
②拿着这个值依次和case后面的值进行对比,一旦有匹配的,就执行对应的语句,在执行过程中,遇到break就结束
③如果所有的case都不匹配,就执行语句体n+1
tips:
①表达式可以是 byte , short , int , char(JDK5以后是枚举,JDK7以后可以是字符串)
②case后面的值:是用来和表达式的值进行匹配的
③break:表示中断的意思
④default:所有的值和表达式都不匹配,则执行default对应的内容