Java小整理

環境設置&檢測

Download~~

請先到以下版本下載jdk包(假如你想要寫產品發佈的話請改用openjdk像red hat發布的,不然某o公司的法顧也是滿強的)
https://www.oracle.com/java/technologies/downloads/

MacOS

Mac 就是讚啦一路按到底就可以了

x86-64架構(intel處理器)選最下面的
arm架構(apple m1/m1 pro/m1 max)的選第二個


不知道處理器是哪個的可以在這裡看: 左上角apple標誌>關於這台mac>處理器

像我的就是intel的
打開它



檢測~~
請在「終端機」輸入2個指令:
java --version
javac --version
出現類似的文字就是設置完成囉

Windows系統

選二或三都可以,以下為第二個的示範


下載完後把它點開

然後照預設值按到底

當出現這個畫面就是安裝完成了

檢驗

同時按windows和r鍵輸入cmd叫出終端機

一樣輸入java --versionjavac --version檢測是否有設好環境變數,新版的應該會自己設定好,如果失敗跳到這裡解決


跳出這個結果就代表安裝成功了。


Trouble shooting

環境變數設置

若發現環境變數設置失敗,則進行下列步驟執行設置

  1. 開啟設置

  2. 點「系統」 > 左側選單的「關於」> 右側「進階系統設定」



  3. 「環境變數設置」> 先點系統變數中的path再點編輯

  4. 貼上java的安裝位址

  5. 執行檢測步驟

Helloworld.java

完成系統變數的設置之後,即可開始編譯了
首先開啟任何一個編輯器NOTEPAD++, VSCODE, 等等皆可
檔名設置為 Helloworld.java (一定要一樣,不然會跳錯),然後輸入下列內容

public class Helloworld{ public static void main(String[] argv){ System.out.println("helloworld!"); } }

存檔,打開終端機
用cd前往當前目錄後
輸入javac Helloworld.java
然後呼叫java來執行java Helloworld


考前整理

class: 為定義任何Java程式之必要關鍵字
class的命名:大寫開頭,避免用到保留字(\關鍵字)keywords
public: 讀取權限的設定,使此程式可被外界讀取及執行
main methos:為程式的主方法,

How to compile:

#編譯指令 javac Welcome1.java #執行指令 java Welcome1

在執行編譯指令產出中介碼.class後,須交由每個機器上的JVM來執行,在JVM執行時會從main開始

列印指令

/* This is comments, the belows are function that print the "String" on the DOS*/ System.out.print(); System.out.println(); System.out.printf();

跳脫字元

再格式化列印的函式中使用:System.out.printf()
%s 代表輸出字串,%d代表十進位數字,%f代表浮點數,%%代表“%”符號,%n代表換行符號

型態

基本資料型態PDT, primitive data type

數字、布林職等

物件型態

String字串型態

算術運算

=為指定運算子Assignment Operator
(double) 為型態指定運算子(casting operator)

Statements

Sequential Structure

The structure that execute the statements from top to down

Selecction Structure

The structure that conditionally execete the statements

  • if
  • if-else
  • nested if
  • switch
  • shorthand if-else Syntax
    • condition > (when true execute):(when false execute);
Equity and relational operator關係運算子

Repetition Structure

  • while
  • for
  • do-while
Compound Assignment operator 複合指定運算子
Increment and decrement operators 遞增遞減運算子

Scanner 物件

載入方法

import java.util.Scanner;

使用方法

import java.util.Scanner; /*main裡:*/ Scanner s = new Scanner(System.in); //創造一個物件s來監聽來自系統匯流排的資料 int a= s.nextInt(); // 收到的視為整數指定予a String txt = s.nextLine(); //讀入一整行,視為字串指定予txt float f = s.nextDouble(); //讀入浮點數,指定予f while(s.hasNext()){ //當可以持續讀入時,執行while loop中的述句 }

Random物件

載入方法

import java.util.Random;

usage

import java.util.Random; public class Demo{ public static void main(String args[]){ Random r = new Random(); int point = r.nextInt(6)+1; } }