算數操作符
+ - * / % ++ --
如果任何運算單元的長度都不超過int,那麼運算結果就按照int來計算
public class HelloWorld {
public static void main(String[] args) {
byte a = 1;
byte b= 2;
byte c = (byte) (a+b); //雖然a b都是byte類型,但是運算結果是int類型,需要進行強制轉換
int d = a+b;
}
}
% 取餘數,又叫取模
i++; 先取值,再運算
++i; 先運算,再取值
關系操作符
關系操作符:比較兩個變量之間的關系
傳回true或false
邏輯操作符
位操作符
指派操作符
三元操作符
Scanner
import java.util.Scanner;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
System.out.println("第一個整數:"+a);
int b = s.nextInt();
System.out.println("第二個整數:"+b);
}
}
nextInt僅僅讀取數字資訊,而不會讀取回車換行"\r\n".
讀取了整數後,接着讀取字元串,那麼就應該連續執行兩次nextLine()