Java基础语法相关练习
test1
需求:判断101-200之间有多少个素数,并输出所有素数
public static void main(String[] args) {
int count = 0;
for (int a = 101; a < 201 ; a++) {
boolean flag = true;
for (int i = 2; i < a / 2; i++) {
if (a % i == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(a);
count++;
}
}
System.out.println("101-200一共有" + count + "个素数");
}
运用flag作为标志可以简化代码
test2
需求:定义方法实现随机产生一个5位的验证码 长度为5 前四位是大写字母和小写字母 最后一位是数字
public static void main(String[] args) {
char[] verificationCode = getVerificationCode();
for (int i = 0; i < verificationCode.length; i++) {
System.out.print(verificationCode[i]);
}
}
public static char[] getVerificationCode() {
Random r = new Random();
char[] basicElement = new char[62];
//将基础元素存入数组
for (int i = 0; i < basicElement.length; i++) {
if(i <= 25) {
basicElement[i] = (char)(97 + i);//a-z
} else if (i > 25 && i <= 51) {
basicElement[i] = (char)(65 + i -26);//A-Z
} else {
basicElement[i] = (char)(48 + i - 52) ;//0-9
}
}
char[] arr = new char[5];
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = basicElement[r.nextInt(52)];
}
arr[arr.length - 1] = basicElement[r.nextInt(10) + 52];
return arr;
}
实现原理:将大写字母、小写字母和数字存入一个数组中,在通过生成随机数成为数组的索引从而起到随机效果
test3
需求: 某系统的数字密码(大于0),比如1983,采用加密方式进行传输 规则如下: 先得到每位数 然后每位数都加上5 再对10求余 最后将所有数字反转 得到一串新数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
int password = sc.nextInt();
int length = getPasswordArrayLength(password);
System.out.println("密码长度为:" + length);
int[] passwordArray = getPasswordArray(length, password);
encryption(passwordArray);
}
//获取密码长度
public static int getPasswordArrayLength(int password) {
int count = 0;
while (password != 0) {
count++;
password /= 10;
}
return count;
}
//将密码拆开存入数组中
public static int[] getPasswordArray(int length, int password) {
int[] passwordArray = new int[length];
for (int i = passwordArray.length - 1 , a = 1; i >= 0; i--, a *= 10) {
passwordArray[i] = password / a % 10;
}
System.out.print("原密码为:");
for (int i = 0; i < passwordArray.length; i++) {
System.out.print(passwordArray[i]);
}
System.out.println();
return passwordArray;
}
//加密
public static void encryption(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] += 5;
arr[i] %= 10;
}
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int number = arr[0];
for (int i = 1; i < arr.length; i++) {
number = number * 10 + arr[i];
}
System.out.println("加密后的密码为:" + number);
}
test4
需求: 双色球 投注号码由6个红色求号码和1个蓝色球号码组成 红色球号码从1-33中选择 蓝色号码球从1-16中选择 一等奖:6+1 二等奖:6+0 三等奖:5+1 四等奖:5+0/4+1 五等奖:4+0/3+1 六等奖:2+1/1+1/0+1
public static void main(String[] args) {
//选择号码
int[] choiceArray = getchoiceArray();
System.out.println("您选择的号码为:");
for (int i = 0; i < choiceArray.length; i++) {
System.out.print(choiceArray[i] + " ");
}
System.out.println();
//生成奖池
int[] awardArray = getAwardArray();
System.out.println("奖池号码为:");
for (int i = 0; i < awardArray.length; i++) {
System.out.print(awardArray[i] + " ");
}
System.out.println();
//判断奖金
getAward(choiceArray, awardArray);
}
//选择号码
public static int[] getchoiceArray() {
Scanner sc = new Scanner(System.in);
int[] arr= new int[7];
System.out.println("请输入您的选择的红球号码:");
for (int i = 0; i < arr.length - 1; ) {
arr[i] = sc.nextInt();
//判断输入的号码是否正确
if (arr[i] < 1 || arr[i] > 33) {
System.out.println("号码有误,请重新输入");
continue;
}
//判断输入的号码是否重复
boolean flag = false;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
flag = true;
break;
}
}
if (flag) {
System.out.println("号码重复,请重新输入");
} else {
i++;
}
}
System.out.println("请输入您选择的蓝球号码:");
while (true) {
arr[arr.length - 1] = sc.nextInt();
//判断输入的号码是否正确
boolean flag = false;
if (arr[arr.length - 1] < 1 || arr[arr.length - 1] > 16) {
flag = true;
}
if (flag) {
System.out.println("号码错误,请重新输入");
} else {
break;
}
}
return arr;
}
//生成奖池
public static int[] getAwardArray() {
Random r = new Random();
int[] arr= new int[7];
for (int i = 0; i < arr.length - 1; ) {
arr[i] = r.nextInt(33) + 1;
//判断新的号码是否与前面的重复
boolean flag = true;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
flag = false;
break;
}
}
if (flag) {
i++;
}
}
arr[arr.length - 1] = r.nextInt(16) + 1;
return arr;
}
//中奖统计
public static void getAward(int[] choiceArray, int[] awardArray) {
//统计红球
int redCount = 0;
for (int i = 0; i < choiceArray.length - 1; i++) {
boolean flag = false;
for (int j = 0; j < awardArray.length - 1; j++) {
if (choiceArray[i] == awardArray[j]) {
flag = true;
break;
}
}
if (flag) {
redCount++;
}
}
//统计蓝球
int blueCount = 0;
if (choiceArray[choiceArray.length - 1] == awardArray[getAwardArray().length - 1]) {
blueCount++;
}
System.out.println("红球中" + redCount + "个");
System.out.println("蓝球中" + blueCount + "个");
//判断奖金
if (blueCount + redCount == 7) {
System.out.println("一等奖,1000万元!");
} else if (blueCount == 6 && redCount == 0) {
System.out.println("二等奖,500万元!");
} else if (blueCount == 5 && redCount == 1) {
System.out.println("三等奖,3000元!");
} else if (blueCount + redCount == 5) {
System.out.println("四等奖,200元!");
} else if (blueCount + redCount == 4) {
System.out.println("五等奖,10元!");
} else if (blueCount == 0 && redCount == 0) {
System.out.println("倒霉鬼没中奖!");
} else {
System.out.println("六等奖,5元!");
}
}
test5
需求: 抢红包 {2,588,888,1000,10000}五个现金红包 请使用代码模拟抽奖,打印出每个奖项 奖项出现的顺序要随机且不重复
public static void main(String[] args) {
int[] pond = {2, 588, 888, 1000, 10000};
lottery(pond);
}
public static void lottery(int[] arr) {
Random r = new Random();
//随机数抽取索引,每次取完索引的范围-1,并将抽到的元素与后面的元素交换位置
for (int i = 0; i < arr.length - 1; i++) {
int index = r.nextInt(arr.length - i);
System.out.println(arr[index]);
int temp = arr[index];
arr[index] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
//最后一个自己手动输出
System.out.println(arr[0]);
}