您的位置:首页 > 其它

利用随机数函数生成 6 个字符,依次拼接到“Words:”字符串的末尾, 并输出该字符串的长度,大写英文字母的个数。

2016-04-07 21:38 1206 查看
/*利用随机数函数生成 6 个字符,依次拼接到“Words:”字符串的末尾,
并输出该字符串的长度,大写英文字母的个数。
随机数函数:Math.random() 获取 0-1 之间的小数。
请查阅 API:java.lang.Math*/
public class WordsJoint {
public static void main(String[] args) {
WordsJoint w = new WordsJoint();
w.produce();
}

public void produce() {
StringBuffer str = new StringBuffer("Words:");
int count = 0;
char c;
for (int i = 0; i < 6; i++) {
c = (char) (Math.random() * 128);
System.out.println(c);
str.append(c);
if (Character.isUpperCase(c))
count++;
}

System.out.println(str + "    生成的大写字母的个数是:" + count + "   字符串的长度是:"
+ str.length());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息