You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java数组操作菜单:如何判断数组已创建并实现创建限制

问题:如何在数组操作菜单中添加数组已创建的错误提示?

我正在开发一个数组操作菜单,当前在「创建数组」选项遇到问题。该选项要求输入数组大小,若大小小于5或大于20需提示错误,这部分我已实现,但还需添加数组已创建时的错误提示功能,请问该如何实现?

以下是我的现有代码:

class process{
 Scanner in = new Scanner(System.in);
 int choice1 =0;
 int limit1;
 int array[];
 public void m(){
 System.out.println(" ARRAY OPERATION");
 System.out.println(" menu");
 System.out.println(" [1] Create Array");
 System.out.println(" [2] Insert Element");
 System.out.println(" [3] Search");
 System.out.println(" [4] Display");
 System.out.println(" [5] Delete");
 System.out.println(" [0] Stop");
 System.out.print(" Enter choice:");
 e();
 }
 public void e(){
 choice1 = in.nextInt();
 cls();
 switch(choice1){
 case 1: {
 System.out.println("Create Array");
 System.out.println("Enter the limit of your array: ");
 limit1 = in.nextInt();
 if(limit1 <5){
 System.out.println("Error: Minimum limit exceeded");
 System.out.println("Going back to main menu");
 m(); //loop to main menu
 } else if(limit1 >20){
 System.out.println("Error: Maximum limit exceeded");
 System.out.println("Going back to main menu");
 m(); //loop to main menu
 } else if(limit1 >=5 || limit1 <=20){
 System.out.println("An array with a limit of " + limit1 + " has been created");
 array = new int[limit1];
 m();
 }
 }
 }

回答

嘿,这个需求很好解决!核心思路就是在创建数组前先检查数组是否已经被初始化——因为Java里数组属于引用类型,默认值是null,所以我们可以通过判断array != null来确定数组已经存在了。

修改后的代码(标注了新增/优化的部分)

class process{
 Scanner in = new Scanner(System.in);
 int choice1 =0;
 int limit1;
 int array[];
 public void m(){
 System.out.println(" ARRAY OPERATION");
 System.out.println(" menu");
 System.out.println(" [1] Create Array");
 System.out.println(" [2] Insert Element");
 System.out.println(" [3] Search");
 System.out.println(" [4] Display");
 System.out.println(" [5] Delete");
 System.out.println(" [0] Stop");
 System.out.print(" Enter choice:");
 e();
 }
 public void e(){
 choice1 = in.nextInt();
 // cls(); 保留你原有清屏逻辑
 switch(choice1){
 case 1: {
 System.out.println("Create Array");
 // 新增:先判断数组是否已创建
 if(array != null) {
     System.out.println("Error: Array has already been created!");
     System.out.println("Going back to main menu");
     m();
     return; // 提前退出,避免执行后续逻辑
 }
 System.out.println("Enter the limit of your array: ");
 limit1 = in.nextInt();
 if(limit1 <5){
 System.out.println("Error: Minimum limit exceeded");
 System.out.println("Going back to main menu");
 m();
 } else if(limit1 >20){
 System.out.println("Error: Maximum limit exceeded");
 System.out.println("Going back to main menu");
 m();
 } else {
 // 优化:无需再重复判断范围,前面已过滤不符合的情况
 System.out.println("An array with a limit of " + limit1 + " has been created");
 array = new int[limit1];
 m();
 }
 }
 // 补充:处理退出和无效输入逻辑,让菜单更完整
 case 0:
     System.out.println("Exiting program...");
     System.exit(0);
 default:
     System.out.println("Invalid choice! Please try again.");
     m();
 }
 }
}

关键逻辑说明

  1. 数组已创建判断:在用户输入数组大小之前,先检查array != null。如果数组已经被初始化(也就是之前创建过),直接提示错误并返回主菜单。
  2. 代码简化:原来的else if(limit1 >=5 || limit1 <=20)可以改成else,因为前面的两个条件已经过滤了<5和>20的情况,剩下的必然是符合要求的范围,代码更简洁。
  3. 完善菜单逻辑:补充了case 0的退出功能和默认分支的无效输入提示,避免程序陷入无限循环或无响应状态。

内容的提问来源于stack exchange,提问作者Reevs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:22:49