java:如何确认数组在“数组操作菜单”中是否已创建

huangapple go评论75阅读模式
英文:

java: How do I confirm an array is already created in an Array Operation Menu

问题

所以我正在制作这个数组操作菜单,我在第一个选项上遇到问题,那就是“创建数组”。我应该在这里输入数组的大小,但如果它小于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("                        菜单");
      System.out.println("                 [1] 创建数组");
      System.out.println("                 [2] 插入元素");
      System.out.println("                 [3] 搜索");
      System.out.println("                 [4] 显示");
      System.out.println("                 [5] 删除");
      System.out.println("                 [0] 停止");
      System.out.print("                 请输入选择:");
      e();
   }

   public void e(){
      choice1 = in.nextInt();
      cls();

      switch(choice1){
         case 1:
         {
            System.out.println("创建数组");
            System.out.println("请输入数组的大小:");
            limit1 = in.nextInt();

            if(limit1 < 5){
                System.out.println("错误:超出最小限制"); 
                System.out.println("返回主菜单");
                m(); //回到主菜单
            }
            else if(limit1 > 20){
                System.out.println("错误:超出最大限制");
                System.out.println("返回主菜单");
                m(); //回到主菜单
            }
            else if(limit1 >= 5 || limit1 <= 20){
               System.out.println("已创建大小为 " + limit1 + " 的数组");
               array = new int[limit1];
               m();
            }
         }
      }
   }
}
英文:

so I'm making this array operations menu and I'm having problems on my first option which is to "create an array". I'm supposed to input the size of the array here, but if it's < 5 or > 20 it will send an error message, that I can do. But I'm also supposed to to show an error message if an array is already created. How can I do that? thanks for the response.

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

答案1

得分: 0

case 1: 中添加

   if (array != null) {
System.out.println("已存在");
break;
}
英文:

add in case 1:

   if (array != null) {
System.out.println(&quot;Already exists&quot;);
break;
}

huangapple
  • 本文由 发表于 2020年10月19日 19:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64426590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定