# **java的小天地** ###### tags: `Java` ### 運算子 1.算術運算子: ![](https://i.imgur.com/CdzxJjI.png) 2.單元運算子: ![](https://i.imgur.com/W1zz58Q.png) 3.關係運算子: ![](https://i.imgur.com/gArQKoI.png) 4.條件運算子: ![](https://i.imgur.com/T9A36MU.png) 5.instanceof 運算子: ![](https://i.imgur.com/fTF4NRE.png) 6.位元運算子: ![](https://i.imgur.com/1s0NUjg.png) 7.指派運算子: ![](https://i.imgur.com/b3hbdk4.png) 8.運算子結合後運算的優先次序: ![](https://i.imgur.com/6qq8FcL.png) 9.中英文術語對照: ![]https://i.imgur.com/FDF2Vju.png ### 建立主函式 :::success 1 . import java.util.Scanner 只能用在"讀取"的部分 2 . import java.util.* 全部都能用 ::: ```java= import java.util.Scanner; //類似c語言的 include(需要讀取資料的時候需要用到) public class heyy{ public static void main(String[] args) { //主函式內容 } } ``` ### 讀取資料 ```java= //sc 可以改成自己想取的名字,下面的 3-6 行都要改 Scanner sc = new Scanner(System.in); // 掃描輸入 char word = sc.next(); // 讀取字串(沒有空白鍵==c語言的%s) int number = sc.nextInt(); // 讀取數字==c語言的%d float num = sc.nextFloat(); // 讀取浮點數==%f String string = sc.nextLine(); // 讀取字串==gets //印出部分-->有兩種印出方法 System.out.println(word); System.out.printf("%d\n",number); ``` ### 布林運算 ```java= boolean bool; //宣告布林變數 int price = 50; bool = price < 50; if(bool) { //if true System.out.println("but it!!"); } else { //if false System.out.println("too expensive"); } ``` ### **Switch** 用法 :::success 用法跟 C語言 相似 ``` switch(判斷的變數){ case 如果變數為... : 執行動作; ``` ::: ```java= Scanner sc = new Scanner(System.in); int day = sc.nextInt(); switch(day) { case 1: System.out.println("monday"); break; //如果沒有break,就會往下一直執行 case 2 : System.out.println("tuesday"); break; case 3 : System.out.println("wednesday"); break; case 4 : System.out.println("thursday"); break; case 5 : System.out.println("friday"); break; case 6 : System.out.println("satursay"); break; default: System.out.println("no"); break; } ``` ### 亂數 :::success number*=1000.0f; '.0' 跟 'f' 是用來告訴電腦這是 float 型態的 ::: ```java= Scanner sc = new Scanner(System.in); float number = (float)Math.random(); //做成浮點數亂數 number*=1000.0f; //數字0-1000亂數(float) int num = (int)number; //0-1000的亂數(int) // System.out.println(num); while(true) { //無限迴圈 int guess = sc.nextInt(); if(guess==num) { System.out.println("bingo!!"); break; //對了就停止 } else if(guess>num) { System.out.println("too big!!"); } else if(guess<num) { System.out.println("too small!!"); } } ``` ### 三元運算子 :::success (判斷式) ?true :false ; ?如果是真要做什麼 , :如果是假要做什麼 ::: ```java= //區域 99 乘法表 Scanner sc = new Scanner(System.in); int m = sc.nextInt(),n = sc.nextInt(); if(m>n) { int temp=m; m=n; n=temp; } for(int i=m;i<=n;i++) { for(int j=m;j<=n;j++) { System.out.printf("%d*%d=%-2d%c",i,j,i*j,(j==n)?'\n':' '); //印出的格式 } } ``` ### 字串 :::spoiler **字串的多種使用方法** :accept: String str = "milk A"; String st2 = "milk B"; --- **1.比較字串 : (判斷是否相等)** ```str.equals(st2) 或是 str.equals("mill not")``` ``str.compareTo(st2)==0 //大小寫有差,等於 0 --> 相等 `` ```boolean t = str.contains(str2); //大小寫有差(t=0 ->不一樣)``` **2.取字串的字元 :** ```char num = str.charAt(i); //相當於拿出第i個字元``` --- ::: #### 1.字串轉字元 && 轉ASCII的方法 ```java= //題目 : 印出0-9最多的次數 import java.nio.charset.StandardCharsets; //轉換所需的函式庫 import java.util.*; public class nor1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); //字串讀取 int[] times = new int [10]; //計算出現的次數 int max=0; byte[] bytes = str.getBytes(StandardCharsets.US_ASCII); //byte陣列大小的型態 --> 把str這個字串變成ASCII,一個字元一個字元填入 for(int i=0;i<str.length();i++) { if(bytes[i]=='\s') { //如果遇到空白就跳過 continue; } times[bytes[i]-49]++; } for(int i=0;i<10;i++) { if(max<times[i]) { max=times[i]; } } System.out.println(max); } } ``` #### 2.字串反轉 && 字串比較 :::success **反轉字串** 的方法: 由外到內一個一個互換、用字串反轉公式直接帶入、善用StringBuilder物件內容、遞迴、stack ::: ```java= //判斷是不是回文 Scanner sc = new Scanner(System.in); String str = sc.nextLine(),copy_str; //copy_str 用來存反轉後的字串 StringBuilder strb = new StringBuilder(str); //使用 toString() 方法從 StringBuilder 物件中得到字串物件 copy_str = strb.reverse().toString(); if(str.compareTo(copy_str)==0) { //依照字典的順序比較兩個字元串(大小寫有差) System.out.println("Yes"); } else { System.out.println("No"); } ``` ### **stack** 堆疊 :::success 用stack的概念,把各個單字反轉過來 ~ ::: ![](https://i.imgur.com/XU1ssRr.png) ```java= Scanner sc = new Scanner(System.in); String str = sc.nextLine(); Stack stack = new Stack(); int n=0; //紀錄單字有幾個字母 for(int i=0;i<str.length();i++) { if((str.charAt(i)<123 && str.charAt(i)>96) || (str.charAt(i)<91 && str.charAt(i)>64)){ n++; stack.push(str.charAt(i)); } else { for(int j=0;j<n;j++) { System.out.printf("%c",stack.pop()); } System.out.printf("%c",str.charAt(i)); n=0; //換完就歸零 } } ``` ### 新增一種類別 ```java= import java.util.*; class Circle{ //Circle 類別 int num; //半徑 void show_area(){ System.out.printf("The circle has radius of %d and area of %d PI.",num,num*num); } } public class easy1 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int number = sc.nextInt(); Circle row = new Circle(); //宣告 row.num=number; //把半徑傳進副函式 row.show_area(); //執行 } } ``` ### 代換、替換