char[] upperChar = new char[26]; int yuanYinCount = 0; for (int i = 97; i < 123; i++) { // 用ascii码赋值,大写的asscii值是65-90 小写是97-122,比下面的方法起要简洁 // char[] chs = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', // 'W', 'X', 'Y', 'Z' }; upperChar[i - 97] = (char) i; } for (int i = 0; i < 100; i++) { int random = (int) (Math.random() * 26); char myChar = upperChar[random]; // 打印字符 System.out.print(myChar); if (myChar == ('a') || myChar == ('e') || myChar == ('i') || myChar == ('o') || myChar == ('u')) { yuanYinCount++; } } System.out.println("\n\r元音字母个数有:" + yuanYinCount); 方法2: int yuanYinCount = 0; for (int i = 0; i < 100; i++) { int random = (int) (Math.random() * 26 + 97); char myChar = (char) random; // 打印字符 System.out.print(myChar); if (myChar == ('a') || myChar == ('e') || myChar == ('i') || myChar == ('o') || myChar == ('u')) { yuanYinCount++; } } System.out.println("\n\r元音字母个数有:" + yuanYinCount);
随机产生100个小写英文统计原音个数
最新推荐文章于 2021-02-13 18:50:20 发布