java实现随机输出26个小写字母

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hju22/article/details/83451567

java实现随机输出26个小写字母

方法:在’a’加随机偏移量,随机偏移量用Math类的random方法实现

package com.word.word;
//随机输出26个英文字母
public class PrintLowerCaseRandom {
    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            int c='a'+(int)(Math.random()*26);   //'a'会自动转换为int以执行加法
            System.out.print((char)c);   //为了打印字符,必须将c强制转为char
        }
    }
}

结果:
坚持比努力更重要

猜你喜欢

转载自blog.csdn.net/hju22/article/details/83451567