java 生成四位驗證碼

package com.lei.common;

public class VerificationCode {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String sum = "";
		for (int i = 0; i < 4; i++) {
			//也可以隨機生成不同類型組成的四位隨機數
			//int p = getPos();  switch (p)
			switch (i) {

			case 0:
				sum += getUpperCase();
				break;
			case 1:
				sum += gerLowerCase();
				break;
			case 2:
				sum += getNumCase();
				break;
			case 3:
				sum += getUpperCase();
				break;

			}System.out.println(sum);
			
		}
		System.out.println(sum);

	}

	// A-Z
	public static char getUpperCase() {

		return (char) (((int) Math.random() * 26)+65);

	}

	public static char gerLowerCase() {

		return (char) (((int) Math.random() * 26)+ 97);
	}

	public static int getNumCase() {

		return (int)( Math.random() * 10);
	}

	public static int getPos() {
		return (int)( Math.random()*3);

	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章