综合案例[控制语句+数组+方法] 游戏[超市储物柜]

点击查看代码
import java.util.Random;
import java.util.Scanner;

public class ChuWuGui {
    public static int[] Boxs = new int[24];

    public static void main(String[] args) {
        while (true){
            fun();
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入操作对应的数字:");
            int operate = sc.nextInt();
            if (operate == 1) {
                exist();
            } else if (operate == 2) {
                take();
            }  else {
                System.out.println("结束");
                System.exit(0);
            }
            System.out.println("按回车键退出返回");
            sc.nextLine();
            sc.nextLine();
        }
    }

    public static void fun() {
        System.out.println("欢迎进入储物柜管理系统!\n");
        for (int i = 0; i < Boxs.length / 2; i++) {
            if (Boxs[i] == 0) {
                System.out.print("□\t");
            } else {
                System.out.print("■\t");
            }
        }
        System.out.println();
        for (int j = 0; j < Boxs.length / 2; j++) {
            System.out.print((j + 1) + "\t");
        }
        System.out.println();
        System.out.println();
        System.out.println();
        for (int k = Boxs.length / 2; k < Boxs.length; k++) {
            if (Boxs[k] == 0) {
                System.out.print("□\t");
            } else {
                System.out.print("■\t");
            }
        }
        System.out.println();
        for (int l = Boxs.length / 2; l < Boxs.length; l++) {
            System.out.print((l + 1) + "\t");
        }
        System.out.println("\n1,存包\t\t2,取包\t\t0,退出");
    }
    public static void exist() {
        boolean f=false;
        for (int i = 0; i < Boxs.length; i++) {
            if(Boxs[i]==0){
                System.out.println("储物柜号:"+(i+1));
                Random random=new Random();
                int mima= random.nextInt(1000000-100000)+100000;//999999-100000
                Boxs[i]=mima;
                System.out.println("储物柜密码:"+mima);
                f=true;
                break;
            }
        }
        if (f==false){
            System.out.println("箱子已满,请稍后再试...");
        }
    }
    public static void take() {
        System.out.print("请输入你要取的储物柜号:");
        Scanner scanner=new Scanner(System.in);
        int hao=scanner.nextInt();
        if(hao>=24&&hao==0){
            System.out.println("输入错误,无法操作!");
        }else {
            System.out.println("请输入密码:");
            int mima=scanner.nextInt();
            if(Boxs[hao-1]==mima){
                System.out.println("密码正确,请取出您的物品。");
                Boxs[hao-1]=0;
            }else {
                System.out.println("密码或储物柜号错误!");
            }
        }
    }
}

posted on 2024-08-29 17:49  小木不痞  阅读(3)  评论(0编辑  收藏  举报

导航