自动生成密码
import java.util.Random;
import java.util.Scanner;
public class Password3 {
public static void main(String[] args) {
//自动生成密码(可选择复杂等级)
Password3 c4 = new Password3();
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到自动密码生成器");
System.out.println("请输入两个整数,第一个:生成的密码位数 第二个:密码的复杂程度(0-4)");
int x = sc.nextInt();
int y = sc.nextInt();
String p = c4.getPwd(x, y);
System.out.println("生成的密码为: "+p);
}
//当键盘什么都不输入时,默认6位数字,复杂程度零级
public String getPwd() {
return getPwd(6, 0);
}
//两个参数,一个是密码位数,一个是密码复杂程度
public String getPwd(int len, int type) {
Random rand = new Random();
String ps = "";
switch (type) {
case 0://0级,纯数字
String letter0 = "0123456789";//定义一个字符串
char[] cs0 = letter0.toCharArray();//把刚定义的字符串转换为一个字符数组
for (int i = 0; i < len; i++) {//循环,并把随机出来到字符拼接在一起成为密码
ps += String.valueOf(cs0[rand.nextInt(cs0.length)]);
}
break;
case 1://1级,26个小写字母
String letter1 = "abcdefghijklmnopqrstuvwxyz";
char[] cs1 = letter1.toCharArray();
for (int i = 0; i < len; i++) {
ps += String.valueOf(cs1[rand.nextInt(cs1.length)]);
}
break;
case 2://2级,26个字母大小写
String letter2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] cs2 = letter2.toCharArray();
for (int i = 0; i < len; i++) {
ps += String.valueOf(cs2[rand.nextInt(cs2.length)]);
}
break;
case 3://3级,26个字母大小写加数字
String letter3 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char[] cs3 = letter3.toCharArray();
for (int i = 0; i < len; i++) {
ps += String.valueOf(cs3[rand.nextInt(cs3.length)]);
}
break;
default://除了0,1,2,3的其他数字等级,26个字母大小写加数字加特殊符号
String letter4 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*()<>,.~`[]{}-+*&^%$#@!?/";
char[] cs4 = letter4.toCharArray();
for (int i = 0; i < len; i++) {
ps += String.valueOf(cs4[rand.nextInt(cs4.length)]);
}
break;
}
return ps;
}
}
自动生成双色球
public class Twocolorball {
public static void main(String[] args) {
//实例化随机数
Random ran = new Random();
//定义红色球数组
int[] red =new int[6];
//通过循环随机数给数组赋值
ss: for (int i = 0; i < red.length; i++) {
int t = ran.nextInt(1,34);
//通过循环判断是否有重复的,如果有i--,结束本次循环
for (int j = 0; j < red.length; j++) {
if (t==red[j]){
i--;
continue ss;
}
}
red[i]= t;//将判断后不重复的数字赋值给红球数组
}
int[] blue = new int[]{ran.nextInt(1, 17)};
//带颜色输出
System.out.print( "双色球为:"+" \033[31m"+Arrays.toString(red)+"\033[0m");
System.out.println(" \033[36m" + Arrays.toString(blue) + "\033[0m");
}
}
判断数字最大值最小值求和
import java.util.Arrays;
import java.util.Random;
public class Judge {
public static void main(String... args) {
System.out.println("最大值为:"+max(-3, -4, -5,10,10));
System.out.println("最小值为:"+min(-3, -4, -5,10,10));
System.out.println("和为:"+sum(-3, -4, -5,10,10));
var cc = new Judge();
String aa =Arrays.toString(cc.getNums(10));
System.out.println("随机数组为:"+aa);
}
//... 是变相的数组 sum(int... n)就相当于sum(int[ ] n)
public static int sum(int... n) {
int sum = 0;//求和
for (int t : n) {
sum += t;
}
return sum;
}
public static int max(int... n) {
int max = 0;//最大值,判断传入n是否为空,为空为零
if (n.length > 0) {
max = n[0];
}
for (int t : n) {
max = Math.max(max, t);
}
return max;
}
public static int min(int... n) {
int min = 0;//最小值,判断传入n是否为空,为空为零
if (n.length > 0) {
min = n[0];
}
for (int t : n) {
min = Math.max(min, t);
}
return min;
}
public int[] getNums(int len) {
int[] tt = new int[len];
Random rand = new Random();
for (int i = 0; i < len; i++) {
tt[i] = rand.nextInt(1, 101);
}
return tt;
}
}