caseExercise

随机验证码

package com.fan.caseExercise;

import java.util.Random;

public class Demo01 {
    public static void main(String[] args) {
        String code = createCode(5); //输入你想要的验证码位数
        System.out.println(code);
    }
    public static String createCode(int n){
        //1.定义一个for循环,用于控制产生多少位随机字符
        Random random = new Random();
        //3.定义一个String类型的变量,用于记忆产生的每位随机字符
        String code = "";
        for (int i = 1; i <= n; i++) {
            //2.为每一个位置生成一个随机字符
            int r = random.nextInt(3); //随机一个 0 1 2 ,0代表随机数字;1 2代表随机大小写字母
            switch (r){
                case 0:
                    //随即一个数字
                    code += random.nextInt(10);
                    break;
                case 1:
                    //随机一个大写字母 A 65   Z 65+25   (0-25)+ 65
                    char ch1 = (char) (random.nextInt(26) + 65);
                    code += ch1;
                    break;
                case 2:
                    //随机一个小写字母 a 97   z 97+25
                    char ch2 = (char) (random.nextInt(26) + 97);
                    code += ch2;
                    break;
            }
        }
        return code;
    }
}

评委打分

package com.fan.caseExercise;

import java.util.Scanner;

public class Democracy {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入评委的人数:");
        int count = scanner.nextInt();
        //引用方法 (直接输出引用)
        System.out.println(arvergeScore(count));
    }
    public static double arvergeScore (int num) {
        //接收评委打分
        int[] score = new int[num];
        for (int i = 0; i < score.length; i++) {
            System.out.println("请输入第" + (i+1) + "个评委的打分:");
            Scanner scanner1 = new Scanner(System.in);
            int score1 = scanner1.nextInt();
            score[i] = score1;
        }
        //冒泡排序 (升序)
        int x =0;
        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score.length - 1; j++) {
                if (score[j] > score[j+1]){
                    x = score[j];
                    score[j] = score[j+1];
                    score[j+1] = x;
                }
            }
        }
        //舍去极端值,求平均数
        double sum = 0;
        for (int i = 0; i < score.length - 1; i++) {
            if (i != 0){
                sum += score[i];
            }
        }
        //返回结果
        //return sum;
        return 1.0 * (sum / (score.length - 2));
    }
}

数字加密

package com.fan.caseExercise;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        int[] code = new int[4];
        System.out.println("请依次输入您的四位数密码:");
        //对Scanner使用for循环捕捉键盘上的数据并依次交给code数组
        for (int i = 0; i <= 3; i++) {
            Scanner scanneri = new Scanner(System.in);
            code[i] = scanneri.nextInt();
        }
        //遍历加密后的数组
        for (int i:enc(code)){
            System.out.print(i + "\t");
        }
    }
    public static int[] enc (int[] array) {
        int[] x = new int[array.length];//承接加密后的密码
        int j = array.length-1;
        //加密
        for (int i = 0; i < array.length; i++) {
            if (j>=0){
                x[j] = (array[i] + 5) % 10; //对code数组中的每个数字进行+5 再对10求余 最后反转
                j--;
            }
        }
        //返回加密后的密码x
        System.out.println("-------------");
        return x;
    }
}

精简优化版

package com.fan.caseExercise;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        int[] code = new int[4];
        System.out.println("请依次输入您的四位数密码:");
        //对Scanner使用for循环捕捉键盘上的数据并依次交给code数组
        for (int i = 0; i <= 3; i++) {
            Scanner scanneri = new Scanner(System.in);
            code[i] = scanneri.nextInt();
        }
        //遍历加密后的数组
        for (int i:enc(code)){
            System.out.print(i + "\t");
        }
    }
    public static int[] enc (int[] array) {
        int[] x = new int[array.length];//承接加密后的密码
        //加密
        for (int i = 0, j = array.length-1; i < array.length && j>=0; i++,j--) {
                x[j] = (array[i] + 5) % 10; //对code数组中的每个数字进行+5 再对10求余 最后反转
        }
        //返回加密后的密码x
        System.out.println("-------------");
        return x;
    }
}

抽红包

1.0版本(多损耗性能)

package com.fan.caseExercise;

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

public class Demo04 {
    public static void main(String[] args) {
        //设立奖池
        int[] moneys = {9,666,188,520,99999};
        //调用抽奖方法
        draw(moneys);
    }
  public static void draw (int[] arr){
      Scanner sc = new Scanner(System.in);
      System.out.println("请输入任意符号开始抽奖!");
      //抽5次奖
      for (int i = 1; i <= 5; i++) {
          sc.next(); //Enter进行下一步操作 模拟抽奖开始按钮
          while (true) {
              Random r = new Random();
              int index = r.nextInt(arr.length);
              if (arr[index] != 0) { //若等于0表示轮空,系统内部重新抽奖
                  System.out.println("您的中奖金额为:" + arr[index]);
                  arr[index] = 0; //刷新奖池
                  break; //结束本次抽奖
              }
          }
      }
      System.out.print("--------------" + "\n" + "奖池已瓜分完毕!");
  }
}

2.0版本(利用换牌算法实现)

package com.fan.caseExercise;

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

public class Demo05 {
    public static void main(String[] args) {
        //设立奖池
        int[] moneys = {9,666,188,520,99999};
        //调用抽奖方法
        draw(moneys);
    }
    public static void draw (int[] arr){
        //洗牌算法(换牌算法)
        //每次让数组中第 n=arr.length-1 (n--)元素与任意一个元素互换
        //(包括自己与自己互换)(互换后锁定最后一个元素)
        for (int i = arr.length-1; i > 0; i--) { //选取数组中最后一个元素
            Random r = new Random();
            int j = r.nextInt(i+1); //从下标(0~i)中随机选取一个元素
            //arr[i]与arr[j]互换
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入任意字符并按下Enter键开始抽奖!");
        for (int i = 0; i < arr.length; i++) {
            sc.next(); //模拟用户开始抽奖交互按钮
            System.out.print("您的中奖金额为:" + arr[i] + "\n-----------------\n");
        }
        System.out.println("奖池已瓜分完毕!");
    }
}

找质数

package com.fan.caseExercise;

public class Demo06 {
    public static void main(String[] args) {
        System.out.println(prim(101,200));
    }
    public static int prim (int start,int end){
        int count = 0;
        OUT:
        for (int i = start; i <= end ; i++) {
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0){
                    continue OUT;
                }
            }
            System.out.println(i);
            count++;
        }
        System.out.println("质数的个数为:");
        return count;
    }
}

双色球

package com.fan.caseExercise;

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

public class Demo07 {
    public static void main(String[] args) {
        int[] user = DCB();
        int[] lucy = creatLucyNumbers();
        System.out.print("您的号码为:\t");
        PrintArray(user);
        System.out.print("中奖号码为:\t");
        PrintArray(lucy);
        System.out.print("中奖情况为:\t");
        judge(user,lucy);
    }
    //打印数组
    public static void PrintArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < 7; i++) {
            System.out.print(i == 6 ? arr[i] : arr[i] + ",");
        }
        System.out.println("]");
    }
    //读取用户输入的双色球号码
    public static int[] DCB (){
        int[]  dbc= new int[7];
        Scanner sc = new Scanner(System.in);
        Scanner sc1 = new Scanner(System.in);
        //红球
        for (int i = 0; i < 6; i++) {
            while (true) {
                System.out.println("请输入第"+(i+1)+"个红球号码(1~33):");
                int num = sc.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("号码超出范围,请重新输入!");
                }else {
                    if (chRe(dbc,num)){
                        dbc[i] = num;
                        break;
                    }else {
                        System.out.println("号码重复,请重新输入!");
                    }
                }
            }
        }
        //蓝球
        while (true) {
            System.out.println("请输入蓝球号码(1~16):");
            int num = sc1.nextInt();
            if (num < 1 || num > 16) {
                System.out.println("号码超出范围,请重新输入!");
            } else {
                dbc[6] = num;
                break;
            }
        }
        return dbc;
    }
    //检查号码是否重复(T代表不重复)
    public static boolean chRe (int[] arr,int number){
        boolean x = true;
        for (int i = 0; i < 6; i++) {
            if (arr[i] == 0){
                return x;
            }
            if (arr[i] == number){
                x = false;
                return x;
            }
        }
        return x;
    }
    //生成随机中奖号码
    public static int[] creatLucyNumbers(){
        Random r = new Random();
        int[] Lucys= new int[7];
        //随机红球号码
        for (int i = 0; i < Lucys.length - 1; i++) {
            while (true) {
                int LucyNumber = r.nextInt(33) + 1;
                if (chRe(Lucys, LucyNumber)) {
                    Lucys[i] = LucyNumber;
                    break;
                }
            }
        }
        //随机蓝球号码
        Lucys[6] = r.nextInt(16) + 1;;
        return Lucys;
    }
    //检查中奖情况
    public static void judge(int[] userNUmbers,int[] lucyNumbers){
        int redcount = 0;
        int bluecount = 0;
        //判断红球中奖数量
        for (int i = 0; i < userNUmbers.length - 1; i++) {
            for (int j = 0; j < lucyNumbers.length - 1; j++) {
                if (userNUmbers[i] == lucyNumbers[j]){
                    redcount++;
                    break;
                }
            }
        }
        //判断蓝球是否命中
        bluecount = userNUmbers[6] == lucyNumbers[6] ? 1 : 0;
        //判断中奖情况
        if (bluecount == 1){
            switch (redcount){
                case (6):
                    System.out.println("恭喜中奖,金额为:10000000元!");
                    break;
                case (5):
                    System.out.println("恭喜中奖,金额为:3000元!");
                    break;
                case (4):
                    System.out.println("恭喜中奖,金额为:200元!");
                    break;
                case (3):
                    System.out.println("恭喜中奖,金额为:10元!");
                    break;
                case (2):
                case (1):
                case (0):
                    System.out.println("恭喜中奖,金额为:5元!");
                    break;
            }
        }else {
            switch (redcount){
                case (6):
                    System.out.println("恭喜中奖,金额为:5000000元!");
                    break;
                case (5):
                    System.out.println("恭喜中奖,金额为:200元!");
                    break;
                case (4):
                    System.out.println("恭喜中奖,金额为:10元!");
                    break;
                case (3):
                case (2):
                case (1):
                case (0):
                    System.out.println("没中~~~");
            }
        }
    }
}

PS:这么难中的吗?

posted @ 2026-01-30 12:00  Shadow001  阅读(4)  评论(0)    收藏  举报