1

GUIで文字を使用して文字を比較する方法を知りたいだけです。文字をランダムにする方法は次のとおりです。

Random r = new Random();

char c = (char) (r.nextInt(26) + 'a');

次に、それを入力文字と比較したいときは、次のようにします。

char c;
if(x==c) {
    prompt.setText("Correct!");
    prompt.setForeground(Color.GREEN);
}
else if(x > c) {
    prompt.setText("Oops! Letter is lower");                
    prompt.setForeground(Color.RED);                      
}                  
else if(x < c) {                      
    prompt.setText("Oops! Letter is higher");                     
    prompt.setForeground(Color.RED);                  
}

しかし、プログラムを実行するたびに、文字を入力するたびに、結果は「おっと! 文字が高い」という結果になります。

お願い助けて。このプログラムを正しく実行するには?

ありがとう!

4

3 に答える 3

0

これを試して:

Random r = new Random();

        char c = (char) (r.nextInt(26) + 'a');

        if ((int)x ==(int)c) {
            prompt.setText("Correct!");
            prompt.setForeground(Color.GREEN);
        } else if ((int)x >(int)c) {
            prompt.setText("Oops! Letter is lower");
            prompt.setForeground(Color.RED);
        } else if ((int)x <(int)c) {
            prompt.setText("Oops! Letter is higher");
            prompt.setForeground(Color.RED);
        }
于 2013-10-01T14:23:08.083 に答える
0

Character.compare(char x, char y)文字の比較に使用します。int差を示すための値を返します。しかし、大きな疑問、変数とは何xですか? それはint、別のものですか、charそれともまったく異なるものですか。xが変数の場合char、次を使用して比較できます。

たとえば、次のようになります。

 if (Character.compare(x, c) == 0) {
     // they are the same.
 } else if (Character.compare(x, c) >= 0) {
     // x is greater than c
 } else {
     // x is less than c
 }
于 2013-10-01T14:05:11.650 に答える
0

メソッドを使用しchar1.compareTo(char2)ます。あなたが求めている価値観を与えてくれます。

ご希望の方は詳細はこちら。

于 2013-10-01T14:06:51.800 に答える