Java(03)
Java基础语法
运算符
Java中的 运算符分为算数运算符、赋值运算符、关系运算符、逻辑运算符、位运算符、条件运算符几种。
算术运算符
包括+、-、*、/、%、++、–
‘/’的使用
15000/10000 = 1
注:在Java中两个同类型的数运算,结果为同类型,又因为他们默认都是int类型,所以就是1
double a=15000/10000;
double a=15000f/10000f;
'%'的使用
‘%’称为取余运算符或称为取模运算符,运算结果是两数相除的余数。
5%3
3%5
‘++’和‘–’的使用
在原来的变量的基础上自增一或者自减一
int a = 10;
a++;
System.out.println(a)
int a = 10;
a--;
System.out.println(a)
讨论:
++ 或 --放在变量前后的区别
以++为例 --运算符同理
++在后,例如:a++ 表示先取值后自增
int a = 10;
int b = a++;
System.out.println(a);
System.out.println(b);
执行过程: 1:先从内存中取a的值,即10, 2:内存中的变量自加 3:最后赋值
++在前,例如:++a表示先自增再取值
int a = 10;
int b = ++a;
System.out.println(a);
System.out.println(b);
复杂的例子
int i=0;
i=++i+i;
System.out.println(i);
int i=0;
i=i+++i;
System.out.println(i);
字符串中的+号
String str="AB";
String str="1+2="+"3";
System.out.println(str);
String str="1+2="+1+2;
System.out.println(str);
String str=1+2+"=1+2";
System.out.println(str);
赋值运算符
'='的使用
java中用‘=’表示赋值操作
注意:在使用‘=’进行复制操作时,小类型可以直接赋值给大类型。大类型直接赋值给小类型则会报错。
byte a=10;
int b=a;
byte a=10+5;
byte a=10;
a=a+5;
short a=10;
a=a+5;
char a=10;
a=a+5;
short a=10;
short b=5;
short c=a+b;
int a=10;
a=a+5;
int a=10;
byte b=a;
int a=10;
byte b=(byte)a;
int a=127;
byte b=(byte)(a+1); -->-128
‘+=’ ‘-=’ ‘*=’ ‘/=’的使用
int a = 90;
a+=10;
System.out.println(a);
这样写的好处
这样写运算效率高,而且有隐含强转的作用
byte a=10;
a+=10;
关系运算符
运算符 | 含义 |
---|
> | 大于 | < | 小于 | >= | 大于等于 | <= | 小于等于 | == | 等于 | != | 不等于 | instanceof | 是否为某个类的对象 |
关系运算符的运算结果都是boolean类型的值
逻辑运算符
&运算符
与 两边都成立,结果才为真,如果有一边是假,结果就为假
System.out.println(7>2 & 2>7);
|运算符
或 两边只要有一个是真,结果就为真
System.out.println(7>2 | 2>7);
!运算符
!true
!false
&&运算符
短路与运算符
||运算符
短路或运算符
&和&&的区别
前者左边不成立,右边也要进行判断,后者,左边不成立,右边就不判断了
int a=1;
int b=1;
System.out.println(4>10 & b==a++);
System.out.println(a);
int a=1;
int b=1;
System.out.println(4>10 && b==a++);
System.out.println(a);
|和||的区别
前者左边成立,右边也要进行判断,后者,左边成立,右边就不判断了
位运算符
&运算符
与
25&18
11001
10010
-------
10000
|运算符
25|18
11001
10010
----------
11011
~运算符
非运算符(按位取反运算符)
~5
0000 0101
1111 1010
^运算符
异或运算符 相同为0,不同为1
25^18
11001
10010
------
01011
<<运算符
按位左移运算符
有符号的移位操作,将整体二进制码左移指定位数,右侧空位补0
比如 6 0000 0110 6 左移一次 000 01100 12
所以,左移一次相当于乘2
>>运算符
算术右移:有符号的移动操作,将整体二进制码右移指定位数,左侧补符号位, 也就是如果符号位是1就补1符号位是0就补0 比如 0000 0110 >>1 0000 0011 3
? 0000 0011>>1 0000 0001 1 右移一次,相当于除2
>>>运算符
逻辑右移:无符号移动操作,将整体二进制码右移指定位数,左侧用0补充 例如 0000 0110逻辑右移一位为0000 0011 //正数情况和算术右移一样 1111 0000逻辑右移一位为0111 1000 //负数情况会变成正数 附:对byte ,short,char 类型移位操作时,都会自动转成int在进行操作
条件运算符
条件运算符又叫三元表达式、三目运算符
格式: 条件表达式?表达式一:表达式二 过程:当表达式为真,则执行一 当表达式为假,则执行二
int a=10;
int b=12;
int max=a>b?a:b;
System.out.println(max);
流程控制语句
流程控制的三中结构
(1)顺序
(2)选择
(3)循环
选择结构
if语句
? if语句的几种格式
(1) if
if(条件表达式){
分支执行内容;
}
int age = 17;
if(age<18){
System.out.println("未成年");
}
System.out.println("成年");
int age=17;
if(age<18)
System.out.println("未成年");
System.out.println("成年");
(2) if-else
int age=17;
if(age<18) {
System.out.println("未成年");
}else {
System.out.println("成年");
}
(3) if-else if-else
int month=7;
if(month==1) {
System.out.println("一月");
}else if(month==2){
System.out.println("二月");
}else if(month==3) {
System.out.println("三月");
}else {
System.out.println("累了不想弄了");
}
(4)嵌套if
public static void main(String[] args) {
String today="周末";
String weather="晴朗";
if(toDay.equals("周末")) {
if(weather.equals("晴朗")) {
System.out.println("去室外");
}else {
System.out.println("去室内");
}
}else {
System.out.println("上班");
}
}
switch语句
格式:
switch(表达式){
case 取值1:
执行语句;
break;
case 取值2:
执行语句;
break;
case 取值3:
执行语句;
break;
case 取值4:
执行语句;
break;
.........
default:
执行语句;
break;
}
break用于跳出switch。不加的话会从第一个满足条件case开始一直运行到第一个break结束
public static void main(String[] args) {
int month=3;
switch (month) {
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
case 1:
case 2:
case 12:
System.out.println("冬季");
break;
default:
System.out.println("输入有误");
break;
}
}
键盘输入
Scanner
(1)next()方法键盘输入获取一个字符串
(2)nextInt()方法键盘输入获取一个整型数
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入一个成绩");
int score=scan.nextInt();
if(score>=0 && score<=20) {
System.out.println("这孩子完蛋了");
}else if(score>=21 && score<=40) {
System.out.println("这孩子太操心了");
}else if(score>=41 && score<=60) {
System.out.println("赶紧救救吧");
}else if(score>=61 && score<=80) {
System.out.println("祖国的希望");
}else if(score>=81 && score<=100) {
System.out.println("开什么玩笑,一定是抄的");
}else {
System.out.println("范围不正确");
}
scan.close();
}
|