Java基礎-03(01).總結運算子、鍵盤錄入、if語句
1:運算子(掌握)
(1)算術運算子
A:+,-,*,/,%,++,--
B:+的用法
a:加法
b:正號
c:字串連線符
C:/和%的區別
資料做除法操作的時候,/取得是商,%取得是餘數
5/3=1
5%3=2
-5%3=-2;
-5%-3=-2;
/*
運算子:
就是對常量和變數進行操作的符號。
分類:算術運算子,賦值運算子,比較運算子,邏輯運算子,位運算子,三目運算子
算術運算子:
+,-,*,/,%,++,--
注意事項:
A:整數相除只能得到整數。如果想得到小數,必須把資料變化為浮點數型別
B:/獲取的是除法操作的商,%獲取的是除法操作的餘數
*/
class OperatorDemo {
public static void main(String[] args) {
//定義變數
int x = 3; //把3賦值給int型別的變數x
int y = 4;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y); //整數相除只能得到整數
//我就想得到小數,該腫麼辦呢?
//只需要把操作的資料中任意的一個數據變為浮點數
System.out.println(x*1.0/y);
//%的應用
System.out.println(x%y); //得到的是餘數
}
}
D:++和--的用法
a:他們的作用是自增或者自減
b:使用
**單獨使用
放在操作資料的前面和後面效果一樣。
a++或者++a效果一樣。
”開發多用++a“
**參與操作使用
放在運算元的前面:先自增或者自減,再參與操作
int a = 10;
int b = ++a;
放在運算元的後面:先參與操作,再自增或者自減
int a = 10;
int b = a++;
/*
++,--運算子的使用:
單獨使用:
放在運算元的前面和後面效果一樣。(這種用法是我們比較常見的)
參與運算使用:
放在運算元的前面,先自增或者自減,然後再參與運算。
放在運算元的後面,先參與運算,再自增或者自減。
作用:就是對變數進行自增1或者自減1。
*/
class OperatorDemo2 {
public static void main(String[] args) {
//定義兩個變數
int x = 3;
int y = 4;
//字串的拼接
//System.out.println("x:"+x);
//System.out.println("y:"+y);
System.out.println("x:"+x+",y:"+y);
//單獨使用
//x++;
//y--;
++x;
--y;
//System.out.println(x);
System.out.println("x:"+x+",y:"+y);
//意外的型別,常量是不可以這樣做的
//System.out.println(10++);
System.out.println("-------------------");
//參與運算使用
int a = 3;
int b = 4;
//int c = a++;
//int d = b--;
int c = ++a;
int d = --b;
System.out.println("a:"+a); //4, 4
System.out.println("b:"+b); //3, 3
System.out.println("c:"+c); //3, 4
System.out.println("d:"+d); //4, 3
}
}
/*
++,--的練習題
第一題:
int a = 10;
int b = 10;
int c = 10;
a = b++;
c = --a;
b = ++a;
a = c--;
請分別計算出a,b,c的值
第二題:
int x = 4;
int y = (x++)+(++x)+(x*10);
請分別計算出x,y的值
*/
class OperatorTest {
public static void main(String[] args) {
int a = 10;
int b = 10;
int c = 10;
a = b++; //a=10,b=11,c=10
c = --a; //a=9,b=11,c=9
b = ++a; //a=10,b=10,c=9
a = c--; //a=9,b=10,c=8
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
System.out.println("--------------");
int x = 4;
int y = (x++)+(++x)+(x*10);
//4+6+60
//x=5,6
System.out.println("x:"+x);
System.out.println("y:"+y);
}
}
/* 擴充套件:
+的用法:
A:加法
B:正號
C:字串連線符
*/
class OperatorDemo3 {
public static void main(String[] args) {
//加法
System.out.println(3+4);
//正號
System.out.println(+4);
System.out.println('a');
System.out.println('a'+1); //這裡是加法
//字串連線符
System.out.println("hello"+'a'+1);
System.out.println('a'+1+"hello");
}
}
(2)賦值運算子
A:=,+=,-=,*=,/=,%=等
B:=叫做賦值運算子,也是最基本的賦值運算子
int x = 10; 把10賦值給int型別的變數x。
C:擴充套件的賦值運算子的特點
隱含了自動強制轉換。
+=:把左邊的數和右面的數進行相加,然後在賦值給左邊的變數。
左邊必須是一個變數。
面試題:
short s = 1;
s = s + 1;
short s = 1;
s += 1;
請問上面的程式碼哪個有問題?
/*
賦值運算子:
基本的賦值運算子:=
把=右邊的資料賦值給左邊。
擴充套件的賦值運算子:+=,-=,*=,/=,%=
+= 把左邊和右邊做加法,然後賦值給左邊。
*/
class OperatorDemo {
public static void main(String[] args) {
//定義一個變數
int x = 10;
//其他用法
int a,b;
a = b = 10;
System.out.println(a);
System.out.println(b);
System.out.println("-----------");
//定義一個變數
int y = 10;
y += 20;
System.out.println(y);
}
}
/*
面試題:
short s=1;s = s+1;
short s=1;s+=1;
上面兩個程式碼有沒有問題,如果有,那裡有問題。
為什麼第二個木有問題呢?
擴充套件的賦值運算子其實隱含了一個強制型別轉換。
s += 1;
不是等價於 s = s + 1;
而是等價於 s = (s的資料型別)(s + 1);
*/
class OperatorTest {
public static void main(String[] args) {
//short s = 1;
//s = s + 1;
//System.out.println(s);
short s = 1;
s += 1; //好像是 s = s + 1;
System.out.println(s);
}
}
(3)比較運算子
A:==,!=,>,>=,<,<=
B:無論運算子兩端簡單還是複雜最終結果是boolean型別。
C:千萬不要把==寫成了=
/*
比較運算子:
==,!=,>,>=,<,<=
特點:
無論你的操作是簡單還是複雜,結果是boolean型別。
注意事項:
"=="不能寫成"="。
*/
class OperatorDemo {
public static void main(String[] args) {
int x = 3;
int y = 4;
int z = 3;
System.out.println(x == y);
System.out.println(x == z);
System.out.println((x+y) == (x+z));
System.out.println("------------");
System.out.println(x != y);
System.out.println(x > y);
System.out.println(x >= y);
System.out.println(x < y);
System.out.println(x <= y);
System.out.println("------------");
int a = 10;
int b = 20;
//boolean flag = (a == b);
//boolean flag = (a = b); //這個是有問題的,不相容的型別
//System.out.println(flag);
int c = (a = b); //把b賦值給a,然後把a留下來
System.out.println(c);
}
}
(4)邏輯運算子
A:&,|,^,!,&&,||
B:邏輯運算子用於連線boolean型別的式子
C:結論
&:有false則false
|:有true則true
^:相同則false,不同則true。
情侶關係。
!:非true則false,非false則true
&&:結果和&是一樣的,只不過有短路效果。左邊是false,右邊不執行。
||:結果和|是一樣的,只不過有短路效果。左邊是true,右邊不執行。
/*
邏輯運算子:
&,|,^,!
&&,||
特點:
邏輯運算子一般用於連線boolean型別的表示式或者值。
表示式:就是用運算子把常量或者變數連線起來的符合java語法的式子。
算術表示式:a + b
比較表示式:a == b
結論:
&邏輯與:有false則false。 有0則0
|邏輯或:有true則true。 有1則1
^邏輯異或:相同為false,不同為true。
舉例:情侶關係。男男,男女,女男,女女
!邏輯非:非false則true,非true則false。
特點:偶數個不改變本身。
*/
class OperatorDemo {
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)); //false & true = false
System.out.println((a < b) & (a > c)); //true & false = false
System.out.println((a < b) & (a < c)); //true & true = true
System.out.println("---------------");
//|邏輯或
System.out.println((a > b) | (a > c)); //false | false = false
System.out.println((a > b) | (a < c)); //false | true = true
System.out.println((a < b) | (a > c)); //true | false = true
System.out.println((a < b) | (a < c)); //true | true = true
System.out.println("---------------");
//^邏輯異或
System.out.println((a > b) ^ (a > c)); //false ^ false = false
System.out.println((a > b) ^ (a < c)); //false ^ true = true
System.out.println((a < b) ^ (a > c)); //true ^ false = true
System.out.println((a < b) ^ (a < c)); //true ^ true = false
System.out.println("---------------");
//!邏輯非
System.out.println(!(a > b)); //!false = true
System.out.println(!(a < b)); //!true = false
System.out.println(!!(a > b)); //!!false = false
System.out.println(!!!(a > b)); //!!false = true
}
}
/*
&&和&的區別? 同理||和|的區別?
A:最終結果一樣。
B:&&具有短路效果。左邊是false,右邊不執行。
開發中常用的邏輯運算子:
&&,||,!
*/
class OperatorDemo2 {
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)); //false && true = false
System.out.println((a < b) && (a > c)); //true && false = false
System.out.println((a < b) && (a < c)); //true && true = true
System.out.println("----------------");
int x = 3;
int y = 4;
//boolean b1 = ((x++ == 3) & (y++ == 4));
//boolean b1 = ((x++ == 3) && (y++ == 4));
//boolean b1 = ((++x == 3) & (y++ == 4));
boolean b1 = ((++x == 3) && (y++ == 4));
System.out.println("x:"+x);
System.out.println("y:"+y);
System.out.println(b1);
}
}
(5)位運算子(瞭解)
A:^的特殊用法
一個數據針對另一個數據位異或兩次,該數不變
a^b^a得到b
B:面試題
a:請實現兩個變數的交換
**採用第三方變數
**用位異或運算子
左邊a,b,a
右邊a^b
b:請用最有效率的方式計算出2乘以8的結果
2<<3 2(2的3次冪)
位運算子:
&,|,^,~
<<,>>,>>>
>>>:得到都是正數
<<:左移 左邊最高位丟棄,右邊補齊0
>>:右移 最高位是0,左邊補齊0;最高為是1,左邊補齊1
>>>:無符號右移 無論最高位是0還是1,左邊補齊0
/*
位運算子:
&,|,^,~
<<,>>,>>>
>>>:得到都是正數
注意:
要做位運算,首先要把資料轉換為二進位制。
*/
class OperatorDemo {
public static void main(String[] args) {
//&,|,^,~
int a = 3;
int b = 4;
System.out.println(3 & 4);
System.out.println(3 | 4);
System.out.println(3 ^ 4);
System.out.println(~3);
}
}
/*
分析:因為是位運算,所以我們必須先把資料換算成二進位制。
3的二進位制:11
00000000 00000000 00000000 00000011
4的二進位制:100
00000000 00000000 00000000 00000100
&位與運算:有0則0。
00000000 00000000 00000000 00000011
&00000000 00000000 00000000 00000100
-----------------------------------
00000000 00000000 00000000 00000000
結果是:0
|位或運算:有1則1。
00000000 00000000 00000000 00000011
|00000000 00000000 00000000 00000100
-----------------------------------
00000000 00000000 00000000 00000111
結果是:7
^位異或運算:相同則0,不同則1。
00000000 00000000 00000000 00000011
&00000000 00000000 00000000 00000100
-----------------------------------
00000000 00000000 00000000 00000111
結果是:7
~按位取反運算子:0變1,1變0
00000000 00000000 00000000 00000011
~11111111 11111111 11111111 11111100 (補碼)
補碼:11111111 11111111 11111111 11111100
反碼:11111111 11111111 11111111 11111011
原碼:10000000 00000000 00000000 00000100
結果是:-4
*/
/*
^的特點:一個數據對另一個數據位異或兩次,該數本身不變。
*/
class OperatorDemo2 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a ^ b ^ b); //10
System.out.println(a ^ b ^ a); //20
}
}
/*
<<:左移 左邊最高位丟棄,右邊補齊0
>>:右移 最高位是0,左邊補齊0;最高為是1,左邊補齊1
>>>:無符號右移 無論最高位是0還是1,左邊補齊0
面試題:
請用最有效率的方式寫出計算2乘以8的結果?
2 * 8
2 << 3
*/
class OperatorDemo3 {
public static void main(String[] args) {
//<< 把<<左邊的資料乘以2的移動次冪
System.out.println(3 << 2); //3*2^2 = 3*4 = 12;
//>> 把>>左邊的資料除以2的移動次冪
System.out.println(24 >> 2); //24 / 2^2 = 24 / 4 = 6
System.out.println(24 >>> 2);
System.out.println(-24 >> 2);
System.out.println(-24 >>> 2);
}
}
/*
計算出3的二進位制:11
00000000 00000000 00000000 00000011
(00)000000 00000000 00000000 0000001100
>>的移動:
計算出24的二進位制:11000
原碼:10000000 00000000 00000000 00011000
反碼:11111111 11111111 11111111 11100111
補碼:11111111 11111111 11111111 11101000
11111111 11111111 11111111 11101000
1111111111 11111111 11111111 111010(00) 補碼
補碼:1111111111 11111111 11111111 111010
反碼:1111111111 11111111 11111111 111001
原碼:1000000000 00000000 00000000 000110
結果:-6
>>>的移動:
計算出24的二進位制:11000
原碼:10000000 00000000 00000000 00011000
反碼:11111111 11111111 11111111 11100111
補碼:11111111 11111111 11111111 11101000
11111111 11111111 11111111 11101000
0011111111 11111111 11111111 111010(00)
結果:
*/
/*
面試題:
請自己實現兩個整數變數的交換
注意:以後講課的過程中,我沒有明確指定資料的型別,預設int型別。
*/
class OperatorTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a:"+a+",b:"+b);
//方式1:使用第三方變數(開發中用的)
/*
int c = a;
a = b;
b = c;
System.out.println("a:"+a+",b:"+b);
System.out.println("------------");
*/
//方式2:用位異或實現(面試用)
//左邊:a,b,a
//右邊:a ^ b
/*
a = a ^ b;
b = a ^ b; //a ^ b ^ b = a
a = a ^ b; //a ^ b ^ a = b
System.out.println("a:"+a+",b:"+b);
*/
//方式3:用變數相加的做法
/*
a = a + b; //a=30
b = a - b; //b=10
a = a - b; //a=20
System.out.println("a:"+a+",b:"+b);
*/
//方式4:一句話搞定
b = (a+b) - (a=b); //b=30-20=10,a=20
System.out.println("a:"+a+",b:"+b);
}
}
(6)三元運算子
A:格式
比較表示式?表示式1:表示式2;
B:執行流程:
首先計算比較表示式的值,看是true還是false。
如果是true,表示式1就是結果。
如果是false,表示式2就是結果。
/*
單目運算子:~3
雙目運算子:3 + 4
三目運算子:
格式:比較表示式?表示式1:表示式2;
比較表示式:結果是一個boolean型別。
執行流程:
根據比較表示式的計算返回一個true或者false。
如果是true,就把表示式1作為結果。
如果是false,就把表示式2作為結果。
*/
class OperatorDemo {
public static void main(String[] args) {
int x = 100;
int y = 200;
int z = ((x > y)? x: y);
//int z = ((x < y)? x: y);
//int z = ((x == y)? x: y);
//報錯
//int z = ((x = y)? x : y);
System.out.println("z:"+z);
}
}
C:案例:
a:比較兩個資料是否相等
b:獲取兩個資料中的最大值
c:獲取三個資料中的最大值
/*
練習:
獲取兩個整數中的最大值
獲取三個整數中的最大值
比較兩個整數是否相同
*/
class OperatorTest {
public static void main(String[] args) {
//獲取兩個整數中的最大值
int x = 100;
int y = 200;
int max = (x > y? x: y);
System.out.println("max:"+max);
System.out.println("--------");
//獲取三個整數中的最大值
int a = 10;
int b = 30;
int c = 20;
//分兩步:
//A:先比較a,b的最大值
//B:拿a,b的最大值在和c進行比較
int temp = ((a > b)? a: b);
//System.out.println(temp);
int max1 = (temp > c? temp: c);
System.out.println("max1:"+max1);
//一步搞定
//int max2 = (a > b)?((a > c)? a: c):((b > c)? b: c);
//這種做法不推薦。
//int max2 = a > b?a > c? a: c:b > c? b: c;
//System.out.println("max2:"+max2);
System.out.println("--------");
//比較兩個整數是否相同
int m = 100;
int n = 200;
//boolean flag = (m == n)? true: false;
boolean flag = (m == n);
System.out.println(flag);
}
}
2:鍵盤錄入(掌握)
(1)實際開發中,資料是變化的,為了提高程式的靈活性,我們加入鍵盤錄入資料。
(2)如何實現呢?目前就記住
A:導包
import java.util.Scanner;
位置:在class的上邊
B:建立物件
Scanner sc = new Scanner(System.in);
C:獲取資料
int x = sc.nextInt();
/*
為了讓程式的資料更符合開發的資料,我們就加入了鍵盤錄入。
讓程式更靈活一下。
那麼,我們如何實現鍵盤資料的錄入呢?
A:導包
格式:
import java.util.Scanner;
位置:
在class上面。
B:建立鍵盤錄入物件
格式:
Scanner sc = new Scanner(System.in);
C:通過物件獲取資料
格式:
int x = sc.nextInt();
*/
import java.util.Scanner;
class ScannerDemo {
public static void main(String[] args) {
//建立鍵盤錄入資料物件
Scanner sc = new Scanner(System.in);
System.out.println("請你輸入一個數據:");
int x = sc.nextInt();
System.out.println("你輸入的資料是:"+x);
}
}
/*
鍵盤錄入練習:鍵盤錄入兩個資料,並對這兩個資料求和,輸出其結果
*/
import java.util.Scanner;
class ScannerTest {
public static void main(String[] args) {
//鍵盤錄入兩個資料,並對這兩個資料求和,輸出其結果
//建立鍵盤錄入物件
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第一個資料:");
int x = sc.nextInt();
System.out.println("請輸入第二個資料:");
int y = sc.nextInt();
//把鍵盤錄入的資料進行相加即可
int sum = (x + y);
System.out.println("sum:"+sum);
}
}
(3)把三元運算子的案例加入鍵盤錄入改進。
/*
鍵盤錄入練習:鍵盤錄入兩個資料,獲取這兩個資料中的最大值
*/
import java.util.Scanner;
class ScannerTest2 {
public static void main(String[] args) {
//建立鍵盤錄入物件
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第一個資料:");
int a = sc.nextInt();
System.out.println("請輸入第二個資料:");
int b = sc.nextInt();
//獲取這兩個資料中的最大值
int max = (a > b? a: b);
System.out.println("max:"+max);
}
}
/*
鍵盤錄入練習:
鍵盤錄入三個資料,獲取這三個資料中的最大值
鍵盤錄入兩個資料,比較這兩個資料是否相等
*/
import java.util.Scanner;
class ScannerTest3 {
public static void main(String[] args) {
//鍵盤錄入三個資料,獲取這三個資料中的最大值
//建立鍵盤錄入物件
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第一個資料:");
int a = sc.nextInt();
System.out.println("請輸入第二個資料:");
int b = sc.nextInt();
System.out.println("請輸入第三個資料:");
int c = sc.nextInt();
//獲取這三個資料中的最大值
int temp = ((a > b)? a: b);
int max = (temp > c? temp : c);
System.out.println("max:"+max);
System.out.println("------------------");
//鍵盤錄入兩個資料
System.out.println("請輸入第一個資料:");
int x = sc.nextInt();
System.out.println("請輸入第二個資料:");
int y = sc.nextInt();
//比較這兩個資料是否相等
boolean flag = (x == y);
System.out.println("flag:"+flag);
}
}
3:流程控制語句
(1)順序結構 從上往下,依次執行
/*
流程控制語句:可以控制程式的執行流程。
分類:
順序結構
選擇結構
迴圈結構
順序結構:
從上往下,依次執行。
*/
class ShunXuJieGouDemo {
public static void main(String[] args) {
System.out.println("程式開始了");
System.out.println("我愛Java");
System.out.println("程式結束了");
}
}
(2)選擇結構 按照不同的選擇,執行不同的程式碼
(3)迴圈結構 做一些重複的程式碼
4:if語句(掌握)
(1)三種格式
A:格式1
if(比較表示式) {
語句體;
}
執行流程:
判斷比較表示式的值,看是true還是false
如果是true,就執行語句體
如果是false,就不執行語句體
B:格式2
if(比較表示式) {
語句體1;
}else {
語句體2;
}
執行流程:
判斷比較表示式的值,看是true還是false
如果是true,就執行語句體1
如果是false,就執行語句體2
C:格式3
if(比較表示式1) {
語句體1;
}else if(比較表示式2){
語句體2;
}
...
else {
語句體n+1;
}
執行流程:
判斷比較表示式1的值,看是true還是false
如果是true,就執行語句體1
如果是false,就繼續判斷比較表示式2的值,看是true還是false
如果是true,就執行語句體2
如果是false,就繼續判斷比較表示式3的值,看是true還是false
...
如果都不滿足,就執行語句體n+1
/*
選擇結構:
if語句
switch語句
if語句:
格式1
格式2
格式3
if語句的格式:
if(比較表示式) {
語句體;
}
執行流程:
先計算比較表示式的值,看其返回值是true還是false。
如果是true,就執行語句體;
如果是false,就不執行語句體;
*/
class IfDemo {
public static void main(String[] args) {
int x = 10;
if(x == 10) {
System.out.println("x等於10");
}
if(x == 20) {
System.out.println("x等於20");
}
System.out.println("over");
}
}
(2)注意事項
A:比較表示式無論簡單還是複雜,結果是boolean型別
B:if語句控制的語句體如果是一條語句,是可以省略大括號的;如果是多條,不能省略。
建議:永遠不要省略。
C:一般來說,有左大括號,就沒有分號,有分號,就沒有左大括號。
D:else後面如果沒有if,是不會出現比較表示式的。
E:三種if語句其實都是一個語句,只要有一個執行,其他的就不再執行。
/*
if語句的注意事項:
A:比較表示式無論簡單還是複雜,結果必須是boolean型別
B:if語句控制的語句體如果是一條語句,大括號可以省略;
如果是多條語句,就不能省略。建議永遠不要省略。
C:一般來說:有左大括號就沒有分號,有分號就沒有左大括號
*/
class IfDemo2 {
public static void main(String[] args) {
int x = 10;
if(x == 10) {
System.out.println("x等於10");
}
if((x > 5) || (x == 10)) {
System.out.println("x大於或者等於10");
}
System.out.println("-------------------");
int a = 100;
/*
if(a == 100) {
System.out.println("a的值是100");
}
*/
if(a != 100) {
System.out.println("a的值是100");
System.out.println("over");
}
System.out.println("-------------------");
int b = 100;
if(b != 100); //這裡其實是有語句體的,只不過是空語句體。
//程式碼塊
{
System.out.println("b的值是100");
System.out.println("over");
}
}
}