一个随机验证码且不重复的小程序以及求随机输入一组数组中的最大值(Java)
1.代码:
package day20181015;
import java.util.Arrays;
/**
* 验证码的实现
* @author Administrator
*/
public class ArrayDemo {
public static void main(String[] args) {
/* 存放所有验证码的字符串 */
String[] pool = { "0", "1", "2", "3", "4", "5", "6", "7", "A", "B",
"C", "F", "T", "S" };// 下标 0~pool.length-1
// 标记上面pool中的元素是否被取过
boolean[] used = new boolean[pool.length];
// 存放验证码
String[] code = new String[6];
int i = 0;// code下标
// 循环产生验证码
while (true) {
// 随机下标
int index = (int) (Math.random() * pool.length);
if (used[index]) {// 字符串被取出过
continue;
}
used[index] = true;
code[i++] = pool[index];// 将随机出来的字符串存放到数组中
if ( i== 6) {
break;
}
}
System.out.println("验证码:" + Arrays.toString(code));
}
}
2.代码‘:
package day20181016;
import java.util.Scanner;
public class MaxDataDemo {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int[]scores=new int[5];
for(int i=0;i<scores.length;i++){
System.out.print("请输入第"+(i+1)+"次成绩:");
scores[i]=input.nextInt();
}
int maxScore=scores[0];
for(int i=1;i<scores.length;i++){
if(scores[i]>maxScore){
maxScore=scores[i];
}
}
System.out.print("五门成绩的最高分为:"+maxScore);
}
}