【发布时间】:2013-10-07 17:39:04
【问题描述】:
我只想问如何在 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);
}
但是,每次我运行我的程序,每输入一个字母,结果都是“糟糕!字母更高”。
请帮帮我。如何正确运行这个程序?
谢谢!
【问题讨论】:
-
x和c来自哪里?从您发布的代码(第二个块)c未初始化。 -
查看小写字母的 ASCII 值。
-
是的,尝试使用 System.out.println 打印 x 和 c。检查值是否在您期望的范围内
-
作为起点,我会将 x 和 c 的值添加到输出中以简化调试。
-
x是否包含大写字母?注意'Z' < 'a'