如何将我的主要方法缩短到它仍然起作用的地方?

How do I shorten my main method to where it still functions?

我有一个 java 代码来读取整数数组的长度,输出范围、间隙长度以及内部的任何不同元素。此外,它将再次输出带有 none 重复的数字。 我想缩短主要方法的长度。 我的代码产生了正确的输出,只是非常冗长。此外,有没有一种方法可以将此主要方法编辑到不需要对我的其他方法进行重大更改的位置?非常感谢!

package ArrayPrograms;

import java.util.Scanner;

public class WIP{
      
    static int LargestGap(int [] a, int n)
     {
         int diff = Math.abs(a[1] - a[0]);
          for(int i = 1; i < a.length-1; i++)
              if(Math.abs(a[i+1]-a[i]) > diff)
                  diff = Math.abs(a[i+1] - a[i]);
          return diff;
     }
    
    int range(int a[], int n)
        {
        
        int max1 = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (Math.abs(a[i] - a[j]) > max1)
                {
                    max1 = Math.abs(a[i] - a[j]);
                }
            }
        }
            return max1;
        
        }
            
    int numberOfDistinctElement(int a[], int n) 
    {
         int num = 1;
            for (int i = 1; i < n; i++)
            {
                int j = 0;
                for (j = 0; j < i; j++)
                    if (a[i] == a[j])
                        break;
                if (i == j)
                    num++;
            }
            return num;
        }
    
    int[] distinctElements(int a[], int n,int numberofDistinct)
    {
      int index = 0;  
       int[] distinct= new int[numberofDistinct];  
        for (int i = 0; i < n; i++)   
        {   
            int flag = 0;  
            for (int j = 0; j < i; j++){  
                if (a[i] == a[j]){   
                    flag = 1;  
                    break;   
                }  
            }  
            if (flag == 0){   
                distinct[index] = a[i];  
                index++;    
            }  
        }
            return distinct;
        } 
        
    ***public static void main(String[] args) 
        {
        
        Scanner in = new Scanner(System.in);
        int num;

        WIP obj=new WIP();
        System.out.print("Enter the length of the array:");
        num = in.nextInt();
        
        int array[] = new int[num];
        
        System.out.print("Enter the elements of the array: ");
        
        for(int i = 0; i < num; i++)
        {
            array[i] = in.nextInt();
        }
        
        System.out.println("The largest gap in the array is "+WIP.LargestGap(array,num)+".");
        System.out.println("The range of the array is  "+obj.range(array,num)+".");
        
        int numberofDistinct=obj.numberOfDistinctElement(array,num);
        
        System.out.println("The number of distinct elements is  "+numberofDistinct+".");
        
        int[] distinctArray=obj.distinctElements(array,num,numberofDistinct);
        
        System.out.print("The array of distinct elements is ["); 
            
        for (int i = 0; i < distinctArray.length; i++) 
            
            if(i== distinctArray.length-1)
            {
                System.out.print(distinctArray[i]+"]");
            }
            else {
                System.out.print( distinctArray[i]+ ",");  
            }
               
            in.close();
        }
    }***

就像大多数评论所说的那样,您无能为力,但是您可以删除和编辑一些身体不需要的括号。这是它的草稿。除此之外,您唯一可以更改的是将所有 WIP.tests 存储在一个代码块中的变量中,然后在另一个代码块中将它们全部打印出来;这将提高可读性。

public static void main(String[] args) 
{

Scanner in  = new Scanner(System.in);
WIP     obj = new WIP();

System.out.print("Enter the length of the array:");
int num = in.nextInt();
int array[] = new int[num] ;

System.out.print("Enter the elements of the array: ");

for(int i = 0; i < num; i++)
    array[i] = in.nextInt();

System.out.println("The largest gap in the array is " + WIP.LargestGap(array,num) + ".");
System.out.println("The range of the array is  " + obj.range(array,num) + ".");

int numberofDistinct = obj.numberOfDistinctElement( array, num );
System.out.println("The number of distinct elements is  "+numberofDistinct+".");
int[] distinctArray = obj.distinctElements(array,num,numberofDistinct);

System.out.print("The array of distinct elements is ["); 
    
for (int i = 0; i < distinctArray.length; i++) 
    if(i== distinctArray.length-1)
        System.out.print(distinctArray[i]+"]");
    else
        System.out.print( distinctArray[i]+ ",");  
       
    in.close();
}

没问题。给你:

package arrayprograms;

import java.util.Scanner;

public class WIP{

    static int LargestGap(int [] a, int n)
    {
        int diff = Math.abs(a[1] - a[0]);
        for(int i = 1; i < a.length-1; i++)
            if(Math.abs(a[i+1]-a[i]) > diff)
                diff = Math.abs(a[i+1] - a[i]);
        return diff;
    }

    int range(int a[], int n)
    {

        int max1 = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (Math.abs(a[i] - a[j]) > max1)
                {
                    max1 = Math.abs(a[i] - a[j]);
                }
            }
        }
        return max1;

    }

    int numberOfDistinctElement(int a[], int n) 
    {
        int num = 1;
        for (int i = 1; i < n; i++)
        {
            int j = 0;
            for (j = 0; j < i; j++)
                if (a[i] == a[j])
                    break;
            if (i == j)
                num++;
        }
        return num;
    }

    int[] distinctElements(int a[], int n,int numberofDistinct)
    {
        int index = 0;  
        int[] distinct= new int[numberofDistinct];  
        for (int i = 0; i < n; i++)   
        {   
            int flag = 0;  
            for (int j = 0; j < i; j++){  
                if (a[i] == a[j]){   
                    flag = 1;  
                    break;   
                }  
            }  
            if (flag == 0){   
                distinct[index] = a[i];  
                index++;    
            }  
        }
        return distinct;
    } 

    static void showResults(int[] array, int num, WIP obj){
        System.out.println("The largest gap in the array is "+WIP.LargestGap(array,num)+".");
        System.out.println("The range of the array is  "+obj.range(array,num)+".");

        int numberofDistinct=obj.numberOfDistinctElement(array,num);

        System.out.println("The number of distinct elements is  "+numberofDistinct+".");

        int[] distinctArray=obj.distinctElements(array,num,numberofDistinct);

        System.out.print("The array of distinct elements is ["); 

        for (int i = 0; i < distinctArray.length; i++) 

            if(i== distinctArray.length-1)
            {
                System.out.print(distinctArray[i]+"]");
            }
            else {
                System.out.print( distinctArray[i]+ ",");  
            }
    }

    public static void main(String[] args) 
    {

        Scanner in = new Scanner(System.in);
        int num;

        WIP obj=new WIP();
        System.out.print("Enter the length of the array:");
        num = in.nextInt();

        int array[] = new int[num];

        System.out.print("Enter the elements of the array: ");

        for(int i = 0; i < num; i++)
        {
            array[i] = in.nextInt();
        }
        in.close();

        showResults(array, num, obj );
    }
}