Java 程式檢查字元是否為 ASCII 7 位可列印字元
若要檢查輸入的值是否為 ASCII 7 位可列印字元,請檢查字元的 ASCII 值是否大於或等於 32 且小於 127。這些是控制字元。
在此處,我們有一個字元。
char one = '^';
現在,我們透過 if-else 檢查可列印字元的條件。
if (c >= 32 && c < 127) { System.out.println("Given value is printable!"); } else { System.out.println("Given value is not printable!"); }
示例
public class Demo { public static void main(String []args) { char c = '^'; System.out.println("Given value = "+c); if (c >= 32 && c < 127) { System.out.println("Given value is printable!"); } else { System.out.println("Given value is not printable!"); } } }
輸出
Given value = ^ Given value is printable!
讓我們看另一個示例,其中給定的值是一個字元。
示例
public class Demo { public static void main(String []args) { char c = 'y'; System.out.println("Given value = "+c); if ( c >= 32 && c < 127) { System.out.println("Given value is printable!"); } else { System.out.println("Given value is not printable!"); } } }
輸出
Given value = y Given value is printable!
廣告