当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character isMirrored()用法及代码示例


Character 类的 isMirrored(char ch) 方法确定给定(或指定)字符是否按照 Unicode 规范进行镜像。镜像字符的字形必须水平镜像,以便在文本中显示时,它们是从右到左的。

注意:上述方法不能用于处理增补字符。为了处理增补字符,我们可以使用 isMirrored(int) 方法。

用法

public static boolean isMirrored(char ch)

参数

ch: 是需要测试镜像属性的字符。

返回值

isMirrored(char ch) 方法返回一个布尔值,即如果给定(或指定)字符被镜像,则为真。否则,该方法返回 false。

例子1

import java.util.Scanner;
public class JavaCharacterIsMirroredExample1 {
   public static void main(String[] args) {
    // Ask the user for the first input.
	System.out.print("Enter the first character:");
    // Use Scanner class to get the user input.
	Scanner s1 = new Scanner(System.in);
    // Gets a single character.
	char[] value1 = s1.nextLine().toCharArray();
         for (char ch1:value1) {
    // Check whether the user input is mirrored or not.
	boolean check1 = Character.isMirrored(ch1);
    // Print the result.
	if (check1) {
	System.out.println("The first input \'" + ch1 + "\' is mirrored.");
	} else {
	System.out.println("The first input \'" + ch1 + "\' is not mirrored.");
			}
        System.out.print("Enter the second character:");
    // Use Scanner class to get the user input.
	Scanner s2 = new Scanner(System.in);
    // Gets a single character.
	char[] value2 = s2.nextLine().toCharArray();
         for (char ch2:value2) {
    // Check whether the user input is mirrored or not.
	boolean check2 = Character.isMirrored(ch2);
    // Print the result.
	if (check2) {
	System.out.println("The second input \'" + ch2 + "\' is mirrored.");
	} else {
	System.out.println("The second input \'" + ch2 + "\' is not mirrored.");
	         }
                    }
             }
     }
}

输出:

Enter the first character:*
The first input '*' is not mirrored.
Enter the second character:[
The second input '[' is mirrored.

例子2

public class JavaCharacterIsMirroredExample2 {
    public static void main(String[] args) {
      // Create three char primitives ch1, ch2 and ch3.
      char ch1, ch2, ch3;

      // Assign the values to ch1, ch2 and ch3.
      ch1 = '{';
      ch2 = '^';
      ch3 = '&';
      // Create three boolean primitives b1, b2 and b3.
      boolean b1, b2, b3;
      // Check whether ch1, ch2 and ch3 are mirrored and assign the results to b1, b2 and b3.
      b1 = Character.isMirrored(ch1);
      b2 = Character.isMirrored(ch2);
      b3 = Character.isMirrored(ch3);
      String str1 = "The first character '"+ch1 +"' is a mirrored character:" + b1;
      String str2 = "The second character '"+ch2 +"' is a mirrored character:" + b2;
      String str3 = "The third character '"+ch3 +"' is a mirrored character:" + b3;
      // Print the values of b1, b2 and b3.
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

输出:

The first character '{' is a mirrored character:true
The second character '^' is a mirrored character:false
The third character '&' is a mirrored character:false

Java 字符 isMirrored() 方法

Character 类的 isMirrored(int codePoint) 方法确定给定(或指定)字符是否按照 Unicode 规范进行镜像。

镜像字符的字形必须水平镜像,以便在文本中显示时,它们是从右到左的。

用法

public static boolean isMirrored (int codePoint)

参数

codePoint:codePoint 是需要测试的字符。

返回值

isMirrored(int codePoint) 方法返回一个布尔值,即如果给定(或指定)字符被镜像,则返回 true。否则,该方法返回 false。

例子1

public class JavaCharacterisMirroredExample_1 {
    public static void main(String[] args) {
     // Initialize the four codepoints.
	 int codepoint1 = 93;
        int codepoint2 = 121;
        int codepoint3 = 168;
        int codepoint4 = 199;
     // Check whether the codePoints are mirrored or not
         boolean check1 = Character.isMirrored(codepoint1);
         boolean check2 = Character.isMirrored(codepoint2);
         boolean check3 = Character.isMirrored(codepoint3);
         boolean check4 = Character.isMirrored(codepoint4);
      // Print the results.
	if(check1){
System.out.print("The first Codepoint \'"+codepoint1 + "\' is mirrored \n");
        }    else{
System.out.print("The first codepoint \'"+codepoint1 + "\' is not mirrored\n");
		}
		
       if(check2){
System.out.print("The second codepoint \'"+codepoint2 + "\' is mirrored. \n");
        }    else{
System.out.print("The second codepoint \'"+codepoint2 + "\' is not mirrored. \n");
		}
		
       if(check3){
System.out.print("The third codepoint \'"+codepoint3 + "\' is mirrored. \n");
        }     else{
System.out.print("The third codepoint \'"+codepoint3 + "\' is not mirrored. \n");
		}
		
       if(check4){
System.out.print("The fourth codepoint \'"+codepoint4 + "\' is mirrored. \n");
        }     else{
System.out.print("The fourth codepoint \'"+codepoint4+ "\' is not mirrored. \n");
                }	
	}
}

输出:

The first Codepoint '93' is mirrored 
The second codepoint '121' is not mirrored. 
The third codepoint '168' is not mirrored. 
The fourth codepoint '199' is not mirrored.

例子2

public class JavaCharacterisMirroredExample_2 {
     public static void main(String[] args) {
      // Create three int primitives c1, c2 and c3.
      int c1, c2, c3;
      // Assign the values to c1, c2 and c3.
      c1 = 0x0c01;
      c2 = 0x003c; 
      c3 = 0x0078; 
      // Create three boolean primitives b1, b2 and b3.
      boolean b1, b2, b3;
     // Check whether the codePoints c1, c2 and c3 are mirrored or not. 
      b1 = Character.isMirrored(c1);
      b2 = Character.isMirrored(c2);
      b3 = Character.isMirrored(c3);
 String str1 = "The first codePoint '"+c1+"' represents a mirrored character:" + b1;
 String str2 = "The second codePoint '"+c2+"' represents a mirrored character:" + b2;
 String str3 = "The third codePoint '"+c3+"' represents a mirrored character:" + b3;
      
     // Print the values of b1, b2 and b3.
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

输出:

The first codePoint '3073' represents a mirrored character:false
The second codePoint '60' represents a mirrored character:true
The third codePoint '120' represents a mirrored character:false

例子3

import java.util.Scanner;
public class JavaCharacterisMirroredExample_3 {
   public static void main(String[] args) {
    // Ask the user for the first input. 
       System.out.print("Enter the first input:");
    // Use the Scanner class to get the user input.
       Scanner s1 = new Scanner(System.in);
    // Gets the user input.
       char[] value1 = s1.nextLine().toCharArray();
    // Check whether the user input is mirrored or not.
       for (char ch1:value1) {
   boolean result1 = Character.isMirrored(ch1);
    // Print the result.
       if(result1){
System.out.println("The character '" + ch1 + "' is mirrored. ");				
                  }
     else{
System.out.println("The character '" + ch1 + "' is not mirrored.");
           }		
       System.out.print("Enter the second input:");
       Scanner s2 = new Scanner(System.in);
       char[] value2 = s2.nextLine().toCharArray();
       for (char ch2:value2) {
      boolean result2 = Character.isMirrored(ch2);
      if(result2){
System.out.println("The character '" + ch2 + "' is mirrored. ");				
                }
      else{
System.out.println("The character '" + ch2 + "' is not mirrored.");
        }		
      System.out.print("Enter the third input:");
       Scanner s3 = new Scanner(System.in);
       char[] value3 = s3.nextLine().toCharArray();
       for (char ch3:value3) {
      boolean result3 = Character.isMirrored(ch3);
      if(result2){
System.out.println("The character '" + ch3 + "' is mirrored. ");				
                }
      else{
System.out.println("The character '" + ch3 + "' is not mirrored.");
           }		
         }
       }
     }
  }
}

输出:

Enter the first input:[
The character '[' is mirrored. 
Enter the second input:*
The character '*' is not mirrored.
Enter the third input:}
The character '}' is not mirrored.




相关用法


注:本文由纯净天空筛选整理自 Java Character isMirrored() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。