Java学习

学习内容:

1.使用纯随机数发生器生成指定数目随机数

代码示例:

import java.util.Random;
import java.util.Scanner;

public class Rand {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入你想要生成的随机数个数:");
int n,a;
n=sc.nextInt();
System.out.println("请输入你想要几个随机数输出一行:");
a=sc.nextInt();
Random rand =new Random();
int x=rand.nextInt(100);
for(int i=1;i<=n;i++) {
x=(16807*x)%2147483647;
System.out.print(x+ " " );
if(i%a==0)
System.out.println();

}
}
}

运行截图:

 2.生成随机验证码

代码示例:

import java.util.Random;
public class Verificationcode {

public static void main(String[] args) {
int num1,num2,i;
int choice1,choice2;
char c;
String[] verificationcode=new String[4];
char[] letter=new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int[] num=new int[] {1,2,3,4,5,6,7,8,9};
Random rand = new Random();
System.out.print("您的验证码为:");
for(i=1;i<=4;i++) {
choice1=rand.nextInt(2);//1为数字,0为字母
if(choice1==0) {
num2=rand.nextInt(26);
System.out.print(letter[num2]);
}
if(choice1==1) {
num2=rand.nextInt(9);
System.out.print(num2);
}
}
}

}

 运行截图:

原文地址:https://www.cnblogs.com/zyj3955/p/13768150.html