1.輸入輸出
在前面的代碼中,我們總是使用System.out.println()
來向屏幕輸出一些內容。println是print line的縮寫,表示輸出並換行。因此,如果輸出後不想換行,可以用System.out.print()
格式化輸出:
如果要把數據顯示成我們期望的格式,就需要使用格式化輸出的功能。格式化輸出使用System.out.printf()
,通過使用佔位符%?
,printf()可以把後面的參數格式化成指定格式:
public class Main{
public static void main(String[] args){
double d = 3.1415927;
System.out.printf("%.2f",d);
System.out.printf("%.4f",d);
}
}
佔位符 | 說明 |
---|---|
%d | 格式化輸出整數 |
%x | 格式化輸出十六進制整數 |
%f | 格式化輸出浮點數 |
%e | 格式化輸出科學計數法表示的浮點數 |
%s | 格式化字符串 |
輸入: 直接看案例
輸入類型:
方法 描述
nextBoolean() 從用戶輸入中讀取1個 boolean 值
nextByte() 從用戶輸入中讀取1個 byte 值
nextDouble() 從用戶輸入中讀取1個 double 值
nextFloat() 從用戶輸入中讀取1個 float 值
nextInt() 從用戶輸入中讀取1個 int 值
nextLine() 從用戶輸入中讀取1個 String 值
nextLong() 從用戶輸入中讀取1個 long 值
nextShort() 從用戶輸入中讀取1個 short 值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 創建Scanner對象
System.out.print("Input your name: "); // 打印提示
String name = scanner.nextLine(); // 讀取一行輸入並獲取字符串
System.out.print("Input your age: "); // 打印提示
int age = scanner.nextInt(); // 讀取一行輸入並獲取整數
System.out.printf("Hi, %s, you are %d\n", name, age); // 格式化輸出
}
}
首先,我們通過import語句導入java.util.Scanner,import是導入某個類的語句,必須放到Java源代碼的開頭
然後,創建Scanner對象並傳入System.in。System.out代表標準輸出流,而System.in代表標準輸入流。直接使用System.in讀取用戶輸入雖然是可以的,但需要更復雜的代碼,而通過Scanner就可以簡化後續的代碼。
有了Scanner對象後,要讀取用戶輸入的字符串,使用scanner.nextLine()
;要讀取用戶輸入的整數,使用scanner.nextInt()
。Scanner會自動轉換數據類型,因此不必手動轉換。
2.if判斷
if語句的基本語法是:
if (條件) {
// 條件一滿足時執行
}
else if(條件) {
// 條件二滿足時執行
}
else{
// 條件不滿足時執行
}
拓:浮點數相等判斷。浮點數在計算機中常常無法精確表示,並且計算可能出現誤差,因此,判斷浮點數相等用==判斷不靠譜
public class Main {
public static void main(String[] args) {
double x = 1 - 9.0 / 10;
// 利用Math的絕對值之差
if (Math.abs(x - 0.1) < 0.00001) {
System.out.println("x is 0.1");
} else {
System.out.println("x is NOT 0.1");
}
}
}
3.switch語句
switch (option) {
case 3:
...
break;
case 2:
...
break;
case 1:
...
break;
default:
...
break;
}
4.while循環
while循環
在每次循環開始前,首先判斷條件是否成立。如果計算結果爲true,就把循環體內的語句執行一遍,如果計算結果爲false,那就直接跳到while循環的末尾,繼續往下執行。
public class Main {
public static void main(String[] args) {
int sum = 0; // 累加的和,初始化爲0
int n = 1;
while (n <= 100) { // 循環條件是n <= 100
sum = sum + n; // 把n累加到sum中
n ++; // n自身加1
}
System.out.println(sum); // 5050
}
}
while循環是先判斷循環條件,再循環,因此,有可能一次循環都不做。
5.do…while循環
在Java中,while循環是先判斷循環條件,再執行循環。而另一種do while循環則是先執行循環,再判斷條件,條件滿足時繼續循環,條件不滿足時退出。它的用法是:
do {
執行循環語句
} while (條件表達式);
可見,do while循環會至少循環一次。
6.for循環
public class Main {
public static void main(String[] args) {
int[] ns = { 1, 4, 9, 16, 25 };
int sum = 0;
for (int i=0; i<ns.length; i++) {
System.out.println("i = " + i + ", ns[i] = " + ns[i]);
sum = sum + ns[i];
}
System.out.println("sum = " + sum);
}
}
/*
i = 0, ns[i] = 1
i = 1, ns[i] = 4
i = 2, ns[i] = 9
i = 3, ns[i] = 16
i = 4, ns[i] = 25
sum = 55
*/
for each循環
本質上爲增強for循環:用來遍歷集合與數組
格式:
for(集合/數組的數據類型 變量名:集合名/數組名){
執行語句
}
7.break與continue
break:會跳出整個循環,也就是整個循環都不會執行了
continue:提前結束本次循環,直接繼續執行下次循環
8.二維數組
public class Main {
public static void main(String[] args) {
int[][] ns = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
System.out.println(ns.length); // 3
}
}
因爲ns包含3個數組,因此,ns.length爲3
.
訪問二維數組的某個元素需要使用array[row][col],例如:
System.out.println(ns[1][2]); // 7