-- 作者:admin
-- 发布时间:2007/3/20 17:54:36
-- 程序代码——简单的打字测试程序
基本概念:
1)系统生成10个随机的小写字符 2)用户输入字符 3)如果用户输入不是10个字符,系统报错并退出 4)系统显示用户输入的错误字符个数(忽略大小写)
public class exec { public static void main(String[] args) { //定义输入的字符个数 final int inputNumberCount=10; //定义保存随机字符串的数组 char[] cstr=new char[inputNumberCount]; //定义统计错误数量的变量 int errorCount=0; //生成随机字符 for(int i=0;i<cstr.length;i++) { cstr[i]=(char)(\'a\'+(int)(Math.random()*26)); } String str=String.valueOf(cstr); //用户输入字符 String inputStr=javax.swing.JOptionPane.showInputDialog(str); char[] inputCStr=inputStr.toCharArray(); //判断用户输入的字符个数是否达到预期 if(inputCStr.length!=cstr.length) { javax.swing.JOptionPane.showMessageDialog(null,"没有正确输入10个字符!"); System.exit(0); } //统计用户输入的错误字符个数 for(int i=0;i<cstr.length;i++) { if(cstr[i]!=Character.toLowerCase(inputCStr[i])) errorCount++; } //显示结果 javax.swing.JOptionPane.showMessageDialog(null,"错误个数为:"+errorCount); System.exit(0); } }
[此贴子已经被作者于2010-12-12 08:12:53编辑过]
|
-- 作者:hank
-- 发布时间:2007/3/20 21:50:27
--
今天在机房上机,机房里的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编辑过]
|