一、找素数(掌握信号位思想)
判断101到200之间有多少个素数,并输出所有素数。
素数:如果除了1和它本身以外,不能被其它正整数整除,就叫素数。
public class demo1 {
public static void main(String[] args) {
for (int i = 101; i < 200; i++) {
boolean flag = true;
for (int j = 2; j < i / 2; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.print(i + "\t");
}
}
}
}
二、开发验证码
定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母。
import java.util.Random;
public class demo2 {
public static void main(String[] args) {
String YANZHENGMA = random(5);
System.out.println("随机验证码为:" + YANZHENGMA);
}
public static String random(int n) {
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type) {
case 0:
char big = (char) (r.nextInt(26) + 65);
code += big;
break;
case 1:
char small = (char) (r.nextInt(26) + 97);
code += small;
break;
case 2:
int number = r.nextInt(10);
code += number;
break;
}
}
return code;
}
}
三、数组的复制
把一个数组中的元素复制到另一个新数组去。
public class demo3 {
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5};
int[] arr2 = new int[arr1.length];
copy(arr1,arr2);
print(arr2);
}
public static void print(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
}
System.out.println("]");
}
public static void copy(int[] arr1,int[] arr2) {
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
}
四、评委打分
在唱歌比赛中,有6名评委给选手打分,分数范围是0~100之间的整数。选手最后的得分为:去掉最高分、最低分后的四个评委的平均分,请完成上述过程并计算出选手的得分。
import java.util.Scanner;
public class demo4 {
public static void main(String[] args) {
double[] score = new double[6];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 6; i++) {
System.out.println("请您输入第" + (i + 1) + "位评委的分数:");
double a = sc.nextDouble();
score[i] = a;
}
double max = score[0];
double min = score[0];
double sum = score[0];
for (int i = 1; i < score.length; i++) {
if (max < score[i]) {
max = score[i];
}
if (min > score[i]) {
min = score[i];
}
sum += score[i];
}
double result = (sum - max - min)/(score.length - 2);
System.out.println("最终的评分为:" + result);
}
}
五、数字加密
某系统的数字密码:比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数。
import java.util.Scanner;
public class demo5 {
public static void main(String[] args) {
System.out.println("请您输入密码位数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
System.out.println("请您输入密码的第" + (i + 1) + "位");
arr[i] = sc.nextInt();
}
System.out.println("您需要加密的密码为:" );
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
}
System.out.println("]");
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5) % 10;
}
for (int i = 0, j = arr.length - 1; i < j; i++,j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.out.println("您所加密好的的密码为:" );
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
}
System.out.println("]");
}
}
六、双色球系统
import java.util.Random;
import java.util.Scanner;
public class demo1 {
public static void main(String[] args) {
int[] usernumbers = userinputnumbers();
System.out.println("您选择的号码是:");
printnumbers(usernumbers);
int[] luckynumbers = createluckynumbers();
System.out.println("中奖号码是:");
printnumbers(luckynumbers);
judge(usernumbers,luckynumbers);
}
public static int[] createluckynumbers() {
//随机出6个红球数字(范围为1~33,且不重复),一个蓝球数字(范围为1~16)
int[] numbers = new int[7];
Random r = new Random();
for (int i = 0; i < numbers.length - 1; i++) {
boolean flag = true;
while (true) {
int reddata = r.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (numbers[j] == reddata) {
flag = false;
break;
}
}
if (flag) {
numbers[i] = reddata;
break;
}
}
}
numbers[numbers.length - 1] = r.nextInt(16) + 1;
return numbers;
}
public static int[] userinputnumbers(){
//让用户输入自己选择的6个红球号码与1个蓝球号码
int[] numbers = new int[7];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < numbers.length - 1; i++) {
System.out.println("请您输入您所选择的第" + (i + 1) + "个红球号码(范围是1~33):");
numbers[i] = sc.nextInt();
}
System.out.println("请您输入您选择的蓝球号码(范围是1~16):");
numbers[6] = sc.nextInt();
return numbers;
}
public static void printnumbers(int[] arr) {
//将号码打印
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
}
System.out.println("]");
}
public static void judge(int[] usernumbers,int[] luckynumbers) {
//判断中奖情况
int redhitnumbers = 0;
int bluehitnumbers;
for (int i = 0; i < usernumbers.length - 1; i++) {
for (int i1 = 0; i1 < luckynumbers.length - 1; i1++) {
if (usernumbers[i] == luckynumbers[i1]) {
redhitnumbers ++;
break;
}
}
}
bluehitnumbers = usernumbers[6] == luckynumbers[6] ? 1 : 0;
//输出中奖信息
if (redhitnumbers < 3 && bluehitnumbers == 1) {
System.out.println("恭喜您中了5元小奖!");
} else if (bluehitnumbers == 1 && redhitnumbers == 3 || bluehitnumbers == 0 && redhitnumbers == 4) {
System.out.println("恭喜您中了10元小奖!");
}else if (bluehitnumbers == 1 && redhitnumbers == 4 || bluehitnumbers == 0 && redhitnumbers == 5) {
System.out.println("恭喜您中了200元大奖!");
}else if (bluehitnumbers == 1 && redhitnumbers == 5 ) {
System.out.println("恭喜您中了3000元大奖!");
}else if (bluehitnumbers == 0 && redhitnumbers == 6 ) {
System.out.println("恭喜您中了500万元巨奖!!!");
}else if (bluehitnumbers == 1 && redhitnumbers == 6 ) {
System.out.println("恭喜您中了1000万元巨奖!!!");
}else {
System.out.println("很遗憾,您没有中奖,感谢您对慈善事业做出的贡献。");
}
}
}