黑馬程序員Java入門視頻速成篇(適合有java基礎(chǔ)、想二刷Java基礎(chǔ)的同學(xué),

# 概述
? ? # JavaSE ? 應(yīng)用開發(fā)
? ? # JavaEE ? 服務(wù)器開發(fā)
? ? # 編譯:javac main.java
? ? # 編譯指定編碼:javac -encoding utf-8 main.java
? ? # 運(yùn)行:java main
# 基本結(jié)構(gòu)
? ? # # 類
? ? # class init_main{
? ? # ? ? # 主函數(shù) , 初始化函數(shù)
? ? # ? ? public static void main(String[] args){
? ? # ? ? ? ? # 調(diào)用 System 類中的 out 類中的 println 函數(shù)
? ? # ? ? ? ? System.out.println("hello world");
? ? # ? ? }
? ? # }
# 輸入輸出
? ? # 標(biāo)準(zhǔn)輸入
? ? ? ? # 例1:手動關(guān)閉資源
? ? ? ? ? ? # import java.util.Scanner;
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args){
? ? ? ? ? ? # ? ? ? ? Scanner sc = ?new Scanner(System.in);
? ? ? ? ? ? # ? ? ? ? int p = sc.nextInt();
? ? ? ? ? ? # ? ? ? ? System.out.println(p);
? ? ? ? ? ? # ? ? ? ? sc.close();
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? ? ? # 例2:自動關(guān)閉資源
? ? ? ? ? ? # import java.util.Scanner;
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args){
? ? ? ? ? ? # ? ? ? ? try (Scanner sc = new Scanner(System.in)) {
? ? ? ? ? ? # ? ? ? ? ? ? int p = sc.nextInt();
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println(p);
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? # 標(biāo)準(zhǔn)輸出
? ? ? ? # class init_main{
? ? ? ? # ? ? public static void main(String[] args){
? ? ? ? # ? ? ? ? System.out.println("你好,世界!");
? ? ? ? # ? ? }
? ? ? ? # }
# 數(shù)據(jù)類型
? ? # 精度:double > float > long > int > short > byte
? ? # 整型 ?int ? ? -2147483648 ———— 2147483647 ?(10位數(shù))
? ? ? ? # int i = 40;
? ? # 整型 ?long ? ?-9223372036854775808 ———— 9223372036854775807 ?(19位數(shù))
? ? ? ? # long l = 30L;
? ? # 整型 ?short ? -32768 ———— 32767
? ? ? ? # short s = 20;
? ? # 浮點(diǎn)數(shù) float ?-3.401298e-38 ———— 3.402823e+38
? ? ? ? # float f = 3.1415926F;
? ? # 浮點(diǎn)數(shù) double ?-4.9000000e-324 ———— 1.797693e+308
? ? ? ? # double d = 20.2;
? ? # 字符 ?char ? ?0 ———— 65535
? ? ? ? # char c = '中';
? ? # 字節(jié) ?byte ? ?-128 ———— 127
? ? ? ? # byte b = 10;
? ? # 字符串 ?String
? ? ? ? # String name = "tony";
? ? # 布爾 boolean ? true or false
? ? ? ? # boolean o = true;
# 條件語句
? ? # if 分支
? ? ? ? # 例1
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? int a = 10;
? ? ? ? ? ? # ? ? ? ? int b = 120;
? ? ? ? ? ? # ? ? ? ? int c = 100;
? ? ? ? ? ? # ? ? ? ? if (a + b > c){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("分支1, a和b的和為:" + (a + b));
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? else{
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("分支2, a和b的和為:" + (a + b));
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? ? ? # 例2
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? int a = 10;
? ? ? ? ? ? # ? ? ? ? int b = 10;
? ? ? ? ? ? # ? ? ? ? int c = 100;
? ? ? ? ? ? # ? ? ? ? if (!(a + b > c)){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("分支2, a和b的和為:" + (a + b));
? ? ? ? ? ? # ? ? ? ? ? ? return;
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? System.out.println("分支1, a和b的和為:" + (a + b));
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? ? ? # 例3
? ? ? ? ? ? # import java.util.Scanner;
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? System.out.println("小明的成績?yōu)椋?#34;);
? ? ? ? ? ? # ? ? ? ? Scanner sc = ?new Scanner(System.in);
? ? ? ? ? ? # ? ? ? ? int i = sc.nextInt();
? ? ? ? ? ? # ? ? ? ? if (i >=0 && i <= 100){
? ? ? ? ? ? # ? ? ? ? ? ? if (i >= 95){
? ? ? ? ? ? # ? ? ? ? ? ? ? ? System.out.println("獎勵手機(jī)一部");
? ? ? ? ? ? # ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? ? ? else if (i >= 90){
? ? ? ? ? ? # ? ? ? ? ? ? ? ? System.out.println("游樂場玩一天");
? ? ? ? ? ? # ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? ? ? else if (i >= 85){
? ? ? ? ? ? # ? ? ? ? ? ? ? ? System.out.println("100元現(xiàn)金");
? ? ? ? ? ? # ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? ? ? else{
? ? ? ? ? ? # ? ? ? ? ? ? ? ? System.out.println("打一頓");
? ? ? ? ? ? # ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? }else{
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("錄入的成績不合法");
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? sc.close();
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? # switch 分支
? ? ? ? # 例1
? ? ? ? ? ? # import java.util.Scanner;
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? ? ? # ? ? ? ? boolean state = true;
? ? ? ? ? ? # ? ? ? ? while (state){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("選擇分支任務(wù):");
? ? ? ? ? ? # ? ? ? ? ? ? int i = sc.nextInt();
? ? ? ? ? ? # ? ? ? ? ? ? state = taskAllocation(i);
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? sc.close();
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # ? ? static boolean taskAllocation(int value){
? ? ? ? ? ? # ? ? ? ? switch(value){
? ? ? ? ? ? # ? ? ? ? ? ? case 1:
? ? ? ? ? ? # ? ? ? ? ? ? ? ? t1();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? # ? ? ? ? ? ? case 2:
? ? ? ? ? ? # ? ? ? ? ? ? ? ? t2();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? # ? ? ? ? ? ? case 3:
? ? ? ? ? ? # ? ? ? ? ? ? ? ? t3();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? # ? ? ? ? ? ? case 4:
? ? ? ? ? ? # ? ? ? ? ? ? ? ? t4();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? # ? ? ? ? ? ? default:
? ? ? ? ? ? # ? ? ? ? ? ? ? ? System.out.println("輸入錯誤");
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # ? ? static void t1(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t1");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? ? ?
? ? ? ? ? ? # ? ? static void t2(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t2");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? ? ?
? ? ? ? ? ? # ? ? static void t3(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t3");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # ? ? static void t4(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t4");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? ? ? # 例2
? ? ? ? ? ? # import java.util.Scanner;
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? ? ? # ? ? ? ? boolean state = true;
? ? ? ? ? ? # ? ? ? ? while (state){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("選擇分支任務(wù):");
? ? ? ? ? ? # ? ? ? ? ? ? int i = sc.nextInt();
? ? ? ? ? ? # ? ? ? ? ? ? state = taskAllocation(i);
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? sc.close();
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # ? ? static boolean taskAllocation(int value){
? ? ? ? ? ? # ? ? ? ? switch(value){
? ? ? ? ? ? # ? ? ? ? ? ? case 1,2,3,4 :
? ? ? ? ? ? # ? ? ? ? ? ? ? ? switch (value) {
? ? ? ? ? ? # ? ? ? ? ? ? ? ? ? ? case 1 -> t1();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? ? ? case 2 -> t2();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? ? ? case 3 -> t3();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? ? ? case 4 -> t4();
? ? ? ? ? ? # ? ? ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? # ? ? ? ? ? ? default:
? ? ? ? ? ? # ? ? ? ? ? ? ? ? System.out.println("輸入錯誤");
? ? ? ? ? ? # ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # ? ? static void t1(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t1");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? ? ?
? ? ? ? ? ? # ? ? static void t2(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t2");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? ? ?
? ? ? ? ? ? # ? ? static void t3(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t3");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # ? ? static void t4(){
? ? ? ? ? ? # ? ? ? ? System.out.println("執(zhí)行任務(wù) t4");
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? # 與 (&&)
? ? ? ? # class init_main{
? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? # ? ? ? ? int a = 10;
? ? ? ? # ? ? ? ? int b = 120;
? ? ? ? # ? ? ? ? int c = 100;
? ? ? ? # ? ? ? ? int d = 200;
? ? ? ? # ? ? ? ? if (a + b > c && a + b < d){
? ? ? ? # ? ? ? ? ? ? System.out.println("分支1, a和b的和為:" + (a + b));
? ? ? ? # ? ? ? ? }
? ? ? ? # ? ? ? ? else{
? ? ? ? # ? ? ? ? ? ? System.out.println("分支2, a和b的和為:" + (a + b));
? ? ? ? # ? ? ? ? }
? ? ? ? # ? ? }
? ? ? ? # }
? ? # 或 (||)
? ? ? ? # class init_main{
? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? # ? ? ? ? int a = 10;
? ? ? ? # ? ? ? ? int b = 500;
? ? ? ? # ? ? ? ? int c = 100;
? ? ? ? # ? ? ? ? int d = 200;
? ? ? ? # ? ? ? ? if (a + b < c || a + b < d){
? ? ? ? # ? ? ? ? ? ? System.out.println("分支1, a和b的和為:" + (a + b));
? ? ? ? # ? ? ? ? }
? ? ? ? # ? ? ? ? else{
? ? ? ? # ? ? ? ? ? ? System.out.println("分支2, a和b的和為:" + (a + b));
? ? ? ? # ? ? ? ? }
? ? ? ? # ? ? }
? ? ? ? # }
? ? # 非 (!)
? ? ? ? # class init_main{
? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? # ? ? ? ? boolean bool = false;
? ? ? ? # ? ? ? ? if (! bool){
? ? ? ? # ? ? ? ? ? ? System.out.println("分支1");
? ? ? ? # ? ? ? ? }
? ? ? ? # ? ? ? ? else{
? ? ? ? # ? ? ? ? ? ? System.out.println("分支2");
? ? ? ? # ? ? ? ? }
? ? ? ? # ? ? }
? ? ? ? # }
# 循環(huán)
? ? # while 循環(huán)
? ? ? ? # 例1
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? boolean state = true;
? ? ? ? ? ? # ? ? ? ? int n = 0;
? ? ? ? ? ? # ? ? ? ? while (state){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println(n);
? ? ? ? ? ? # ? ? ? ? ? ? n = n + 1;
? ? ? ? ? ? # ? ? ? ? ? ? if (n == 10){
? ? ? ? ? ? # ? ? ? ? ? ? ? ? state = false;
? ? ? ? ? ? # ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? ? ? # 例2
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? boolean state = true;
? ? ? ? ? ? # ? ? ? ? int n = 0;
? ? ? ? ? ? # ? ? ? ? while (state){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println(n);
? ? ? ? ? ? # ? ? ? ? ? ? n = n + 1;
? ? ? ? ? ? # ? ? ? ? ? ? if (n == 10){
? ? ? ? ? ? # ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? # ? ? ? ? ? ? }
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? # for 循環(huán)
? ? ? ? # 無限循環(huán)
? ? ? ? ? ? # class init_main{
? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? # ? ? ? ? for (;;){
? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("你好");
? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? # ? ? }
? ? ? ? ? ? # }
? ? ? ? # 有限循環(huán)
? ? ? ? ? ? # 例1
? ? ? ? ? ? ? ? # class init_main{
? ? ? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? ? ? # ? ? ? ?# 參數(shù)1為初始化變量 , 參數(shù)2為循環(huán)條件 , 條件不成立則終止循環(huán) , 參數(shù)3為更新條件
? ? ? ? ? ? ? ? # ? ? ? ? for (int i = 0; i != 10; i = i + 1){
? ? ? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("你好");
? ? ? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? ? ? # ? ? }
? ? ? ? ? ? ? ? # }
? ? ? ? ? ? # 例2
? ? ? ? ? ? ? ? # class init_main{
? ? ? ? ? ? ? ? # ? ? public static void main(String[] args) {
? ? ? ? ? ? ? ? # ? ? ? ? for (int i = 0; i != 10;){
? ? ? ? ? ? ? ? # ? ? ? ? ? ? System.out.println("你好");
? ? ? ? ? ? ? ? # ? ? ? ? ? ? i = i + 1;
? ? ? ? ? ? ? ? # ? ? ? ? }
? ? ? ? ? ? ? ? # ? ? }
? ? ? ? ? ? ? ? # }
# 函數(shù)
? ? # class init_main{
? ? # ? ? public static void main(String[] args) {
? ? # ? ? ? ? int sun = add(100,200);
? ? # ? ? ? ? System.out.println(sun);
? ? # ? ? ? ? System.out.println(add(100,200));
? ? # ? ? }
? ? ? ?
? ? # ? ? static int add(int a,int b){
? ? # ? ? ? ? return a + b;
? ? # ? ? }
? ? # }