第一章Java基礎
阿新 • • 發佈:2020-07-03
一、eclipse使用
1、快捷鍵
/* * 內容輔助鍵: * A:main方法 * main,然後alt+/,回車 * B:輸出語句 * syso,然後alt+/,回車 * * * 快捷鍵: * A:註釋 * 單行:選中內容,ctrl+/,取消註釋,重複執行 * 多行:選中內容,ctrl+shift+/,取消註釋,ctrl+shift+\ * B:格式化 * ctrl+shift+F * 右鍵,Source,Format*/ public class KeyDemo { public static void main(String[] args) { System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); } }
2、匯入專案問題
不能匯入重名專案
3、算術運算子++和--的用法
package com.itheima_01; /* * ++,--運算子:對變數做加1或者減1的操作。 * ++或者--既可以放在變數的後面,也可以放在變數的前面。 * 單獨使用的時候,++或者--無論是放在變數的前面還是後面,結果是一樣的。 * 參與操作的時候: * 如果++或者--在變數的後面,先拿變數參與操作,後變數做++或者-- * 如果++或者--在變數的前面,先變數做++或者--,後拿變數參與操作 */ public class OperatorDemo4 { public static voidmain(String[] args) { int a1 = 10; int a2 = 10; System.out.println("a1:" + a1); System.out.println("a2:" + a2); // 參與操作使用 int b = a1++; int c = ++a2; System.out.println("b:" + b); System.out.println("c:" + c); } }
結果
a1:10
a2:10
b:10
c:11
4、邏輯運算子&&和&的區別
&&和&的結果一樣
||和|的結果一樣
&&和&的區別:
&&有短路效果,左邊為false,右邊不執行
&左邊無論是什麼,右邊都會執行
同理||和|的區別:
||左邊為true,右邊不執行
|左邊無論是什麼,右邊都會執行
public class OperatorDemo { public static void main(String[] args) { int a = 10; int b = 20; int c = 10; int d = 20; System.out.println((a++ >= 10) | (b++ > 20)); System.out.println((c++ >= 10) || (d++ > 20)); System.out.println("a:" + a); System.out.println("b:" + b); System.out.println("c:" + c); System.out.println("d:" + d); } }
結果:
true
true
a:11
b:21
c:11
d:20
5、鍵盤錄入和基本使用步驟
package com.bird; import java.util.Scanner; /* * 為了提高程式的靈活性,我們就把資料改進為鍵盤錄入。 * 如何實現鍵盤錄入呢?目前我們只能使用JDK提供的類Scanner。 * 這個使用的步驟,目前大家記住就可以了。 * * 使用步驟: * A:導包 * import java.util.Scanner; * 類中的順序:package > import > class * B:建立物件 * Scanner sc = new Scanner(System.in); * C:接收資料 * int i = sc.nextInt(); */ public class ScannerDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("請輸入"); int a = sc.nextInt(); System.out.println("a:"+a); } }
6、if練習
package com.bird_02; import java.util.Scanner; /* * 需求:鍵盤錄入兩個數字,比較大小 * 分析: * A:鍵盤錄入三個步驟 * 導包,建立鍵盤錄入物件,接收資料 * B:判斷哪個數字更大 * C:輸出結果 */ public class ifTest1 { 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; // 判斷哪個數字更大 if (a > b) { max = a; } else { max = b; } // 輸出結果 System.out.println("最大數為" + max); } }
package com.bird_02; /* * 需求: * 鍵盤錄入學生考試成績,根據成績判斷該學生屬於哪個級別 * 90-100【包含90和100】 優秀 * 80-90【包含80不包含90】 好 * 70-80 【包含70不包含80】 良 * 60-70 【包含60不包含70】 及格 * 60分以下 【不包含60】 不及格 * * 分析: * A:鍵盤錄入的三個步驟 * B:使用IF結構3來判斷 */ import java.util.Scanner; public class ifTest2 { public static void main(String[] args) { // 建立鍵盤錄入物件 Scanner sc = new Scanner(System.in); //接收資料 System.out.println("請錄入成績"); int score = sc.nextInt(); if(score>100||score<0){ System.out.println("請錄入正確的成績"); }else if(score<=100 && score >=90){ System.out.println("優秀"); }else if(score<90 && score >=80){ System.out.println("好"); }else if(score<80 && score >=70){ System.out.println("良"); }else if(score<70 && score >=60){ System.out.println("及格"); }else{ System.out.println("不及格"); } } }