今天在机房上机,机房里的JBuilder里面的光标与字符不对应让我不能得心应手地写程序,只能写了一半就交上去了。现在我已经写完了。不知道好不好。现在拿出来希望和大家共享并探讨一下。
import javax.swing.JOptionPane;
public class hello
{
public static void main(String args[]) {
int j,cou=0;
String r = "";
String x,b,c;
for (int i = 0; i < 10; ) {
j = (int) (Math.random() * 200);
if (j > 97 && j < 123) {
i++;
r = r + (char) (j);
}
}
x = JOptionPane.showInputDialog(r);
if (x.length() != 10) {
JOptionPane.showMessageDialog(null, "字数不对!", "Result",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
else if (x.compareTo(r)==0) {
JOptionPane.showMessageDialog(null, "完全匹配!谢谢合作!", "Result",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
else {
for(int i=0;i<10;i++)
{
b=x.substring(i,i+1);
c=r.substring(i,i+1);
if(b.compareTo(c)!=0)
++cou;
}
JOptionPane.showMessageDialog(null, "你输入错了"+cou+"个字母", "Result",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
}
[此贴子已经被admin于2010-12-12 08:13:11编辑过]