zoukankan      html  css  js  c++  java
  • 使用ArrayList对大小写字母的随机打印

    从a~z以及A~Z随机生成一个字母并打印;打印全部的字母

    package com.liaojianya.chapter1;
    
    import java.util.ArrayList;
    
    /**
     * This program demonstrates the way of generating random a~z, A~Z.
     * @author LIAO JIANYA
     *
     */
    public class RandomCharacters
    {
    	public static void main(String[] args)
    	{
    		ArrayList<Character> list = new ArrayList<Character>();
    		for(char i = 'a'; i <= 'z'; i++)
    		{
    			list.add(i);
    		}		
    		for(char i = 'A'; i <= 'Z'; i++)
    		{
    			list.add(i);
    		}
    		String str = "";
    		int num = (int)(Math.random() * 52);
    		System.out.print("第" + num + "个字母为:");
    		str = str + list.get(num);
    		System.out.println(str);
    		System.out.println("----------打印出a~z以及A~Z--------------");
    		for(Character s : list)
    		{
    			System.out.print(s);
    		}
    	}
    }
    

      运行结果

    第3个字母为:d
    ----------打印出a~z以及A~Z--------------
    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    

      这里使用了ArrayList这个动态数组的方法,通过for循环以及add的方法来添加元素;使用增强型for循环,打印出list中所有的元素。

  • 相关阅读:
    Codeforces Round #420 (Div. 2) A-E
    Codeforces Round #419 (Div. 2) A-E
    Bzoj4423 [AMPPZ2013]Bytehattan
    51nod1471 小S的兴趣
    Bzoj2629 binomial
    51nod1056 最长等差数列 V2
    51nod1055 最长等差数列
    51nod1110 距离之和最小 V3
    20. 有效的括号
    155.最小栈
  • 原文地址:https://www.cnblogs.com/Andya/p/5680235.html
Copyright © 2011-2022 走看看