码迷,mamicode.com
首页 > 其他好文 > 详细

2017-11-15

时间:2017-11-15 23:48:58      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:return   大写   class   span   接收   demo   div   软件   转义   

Program:

  根据用户选择,随机产生数字,小写字母,大写字母,字母数字混合

 

代码如下:

 

 1 /*
 2  * Program:根据用户选择随机产生数字,小写字母,大写字母,字母数字混合
 3  * 
 4  * Date Written:2017-11-15(软件测试实验)
 5  * 
 6  * */
 7 
 8 import java.util.Random;
 9 import java.util.Scanner;
10 
11 public class TestDemo {
12 
13     public static void main(String args[]) {
14 
15         char c;                //接收转义后的字符
16         int check = 0;         //接收用户的选择
17         Scanner scan = new Scanner(System.in);
18 
19         while(true) {
20         
21             System.out.println("\n1->数字\n2->大写字母\n3->小写字母\n4->数字字母混合");
22             check = scan.nextInt();          //取得用户输入
23     
24             for (int i = 0; i < 10; i++) {
25     
26                 c = (char) getNum(check);    //取得随机
27                 System.out.print(c + "  ");
28             }
29         }
30 
31     }
32 
33     //取得随机ASCII吗
34     public static int getNum(int n) {
35 
36         Random rand = new Random();
37         int num = 0;       //记录随机产生的ASCII
38         int max = 109;     //max 和 min 初始化为取得数字字母混合条件下的数字
39         int min = 48;        
40         switch (n) {
41 
42         case 1:            //取得数字ASCII
43             max = 57;
44             break;
45         case 2:            //取得大写字母ASCII
46             min = 58;
47             max = 83;
48             break;
49         case 3:            //取得小写字母ASCII
50             min = 84;
51             max = 109;
52             break;
53         }
54 
55         //随机产生指定区间的ASCII,两边都是闭区间
56         num = rand.nextInt(max) % (max - min + 1) + min;
57         //num = rand.nextInt(26) + 97;
58         if (num > 83) {            //产生小写字母的ASCII-13
59 
60             num += 13;
61         } else if (num > 57) {     //产生大写字母的ASCII-7
62 
63             num += 7;
64         }
65         return num;                //产生的随机数为数字
66     }
67 
68 }

 

2017-11-15

标签:return   大写   class   span   接收   demo   div   软件   转义   

原文地址:http://www.cnblogs.com/caizhen/p/7841283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!