【问题标题】:java: How do I confirm an array is already created in an Array Operation Menujava:如何确认数组已在数组操作菜单中创建
【发布时间】:2021-02-02 04:26:57
【问题描述】:

所以我正在制作这个数组操作菜单,但我的第一个选项是“创建数组”时遇到了问题。我应该在这里输入数组的大小,但如果它是 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();
     }
     }

【问题讨论】:

  • if (array == null)!= 你想知道的。

标签: java arrays menu bluej


【解决方案1】:

添加case 1:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2014-12-05
    相关资源
    最近更新 更多