# JAVA 字串比較.equals() / 格式化字串System.out.printf() ###### tags: `Java` --- ## 基本判斷式 ### .equals() * 比較格式類型與值,較為精準 * ==是比較數字 ```java= import java.util.Scanner; public class Ex07_01_equals { public static void main(String[] args){ Scanner scn = new Scanner(System.in); String vip; double money; System.out.println("請輸入購買金額:"); money = scn.nextDouble(); //輸入內容轉為dOUBLE System.out.println("請問是否為VIP(輸入Y/N)"); vip = scn.next(); if(vip.equals("Y") | vip.equals("y")){ //JAVA區分大小寫所以Yy都要檢查 money = money * 0.85; } System.out.printf("需要支付金額: %.1f" , money ); //格式化字串 scn.close(); } } ``` ### 輸入內容類型判別 ```java= import java.util.Scanner; public class Ex07_03_isDigit_isUpperCase { public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("請輸入任一字元:"); char c = scn.next().charAt(0); if (Character.isDigit(c)){ System.out.println("您輸入的字元是數字"); }else if(Character.isUpperCase(c)){ System.out.println("您輸入的字元是大寫字母"); }else if(c>='a' && c<='z'){ System.out.println("您輸入的字元是小寫字母"); }else { System.out.println("您輸入的是其他的字元符號"); } scn.close(); } } ``` ## 巢狀判斷式 ### if/else/if多選一條件 * if裡面有很多個if ```java= import java.util.Scanner; public class Ex07_04_aLotOf_if { public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("請輸入任意3組數字:"); int num1 = scn.nextInt(); int num2 = scn.nextInt(); int num3 = scn.nextInt(); int max; if(num1>num2) { if(num1>num3) max = num1; else max = num3; }else { if(num2>num3) max = num2; else max = num3; } System.out.println("最大的數字是:"+max); scn.close(); } } ``` ### switch多選一條件 ```java= import java.util.Scanner; public class Ex07_05_switch { public static void main(String[] args) { Scanner scn = new Scanner(System.in); char grade; System.out.println("請輸入考試分數: "); int score = scn.nextInt(); System.out.println(score); score = score / 10; //除以10取十位數字,不看個位數字 switch(score){ case 10: case 9: case 8: grade = '甲'; break; case 7: grade = '乙'; break; case 6: grade = '丙'; break; default: grade = '丁'; } System.out.println("屬於"+grade+"等的成績。"); scn.close(); } } ``` --- ## 99乘法表(System.out.printf()) ### System.out.printf() | 格式 | 說明 | | -------- | -------- | | %d | 整數(10進制) | |%o|整數(8進制)| |%x|整數(16進制)| |%X|大寫整數(16進制)| | %n|換行| |%f|浮點數| | %S |字串以大寫输出| | 1$|表示第一个字符串 (3$表示第3个字符串)| |%t|表示日期| 其他詳細可參考[Java 格式化輸出printf 例子 - 菜鳥教程](https://www.runoob.com/w3cnote/java-printf-formate-demo.html) ```java= public class Ex07_09_99 { public static void main(String[] args) { int a = 1, b = 1; while(a<=9) { while(b<=9){ //當B小於等於9 執行下列程式 System.out.printf("%d*%d=%2d " , a, b, a*b); //A跟B的值代入%d b++; } a++; b = 1; System.out.println(); } } } ``` 顯示如下: ``` 1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 2*1= 2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1= 3 3*2= 6 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1= 4 4*2= 8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 Process finished with exit code 0 ``` --- <span class="code1"></span> <style> h2 { color: #2383B8; } h3 { color: #1AA340; } h4 { color: white; background-color: #2383B8; padding:8px; } .code1 { padding: 2px 4px; font-size: 90%; color: #c7254e; background-color: #f9f2f4; border-radius: 4px; font-family:'Fira Code'; } .code { padding: 2px 4px; font-size: 90%; font-family:'Fira Code'; } </style>