Java 中的字符比较

char comparison in Java

class Tryit {   
  public static void main(String args[])   
    throws java.io.IOException { 

    char ch, answer; 

    System.out.println("Please Enter a Secret Character for another user to guess."); 
    answer = (char) System.in.read(); 

    System.out.println("There is a Secret character for you...Can you guess it: \n"); 
    ch = (char) System.in.read(); // read a char from the keyboard 


    if(ch == answer) 

    {   
    System.out.println("** Right **"); 
    }
    else 
    {
    System.out.println("** Try Again **"); 
    }
  }   
}

我正在尝试比较 ch 和 answer 变量,但它不起作用,我在哪里做错了。帮助

使用扫描仪...很简单

然后和==比较,添加了while循环来踢球

class Tryit {   
      public static void main(String args[])   
        throws java.io.IOException { 

        char ch, answer; 
        Scanner scanner = new Scanner(System.in); 
         while(true) {
        System.out.println("Please Enter a Secret Character for another user to guess."); 
        answer = scanner.next().charAt(0);

        System.out.println("There is a Secret character for you...Can you guess it: \n"); 
        ch = scanner.next().charAt(0); 

           if(ch == answer)    
           {   
            System.out.println("** Right **"); 
           }
           else 
          {
          System.out.println("** Try Again **"); 
          } 
        }
      }   
    }