N input string, find the longest shortest string (if the same number, prints out)

    First, the minimum required to find the longest string, we should use an array to save up, the number of inputs is not fixed, we can get the number to be entered with the Scanner, the number eventually found not fixed, we two methods may be encapsulated, and the return value is an array type.

I encountered problems, I began to think of string concatenation, think of what the return value with an array of deposit, each time resulting in the number of returns are fixed, even if there are more.

code show as below:

 

Import java.util.Scanner; 

// input string of n rows, find the longest shortest string (if the same number, prints out) 
public  class the FindString { 

    public  static  void main (String [] args) { 
        Scanner S = new new Scanner (the System.in); 
        System.out.println ( "Please enter a string of n" );
         int n = s.nextInt (); 
        string [] ARR = new new string [n]; 
        s.nextLine ( ); 
        System.out.println ( "Please enter the string" );
         for ( int I = 0; I <n-; I ++ ) { 
            ARR [I] = s.nextLine();
        }

        String[] max = findMax(arr,arr[0]);
        System.out.println("最长的: ");
        for (int i = 0; i < max.length; i++) {
            if(max[i] != null){
            System.out.print(max[i]+" ");
            }
        }
        System.out.println();

        String[] min = findMin(arr,arr[0]);
        System.out.println("最短的:");
        for (int i = 0; i < min.length; i++) {
             IF (min [I] =! Null ) { 
                of System.out.print (min [I] + "" ); 
            } 
        } 
    } 

    / ** 
     * Get the longest 
     * @param ARR input array 
     * @param S the first element of the array 
     * @return   array returned and placed longest substring null
      * / 
    Private  static String [] FindMin (String [] ARR, String S) {
         int min = s.length (); 
        String [] TEMP = new new String [arr.length];
         for ( int. 1 = I; I <arr.length; I ++ ) {
             IF (ARR [I] .length () < min) { 
                min = ARR [I] .length (); 
                S = ARR [I]; 
            } 
        } 
        // go After one of the longest found described, with reference to the length of the array and then traversed again, find its length the same 
        for ( int J = 0; J <arr.length; J ++ ) {
             IF (ARR [J] .length () == s.length ()) { 
                TEMP [J] = ARR [J]; 
            } 
        } 

        return TEMP; 

    } 

    Private  static String[] findMax(String[] arr, String s) {
        int max = s.length();
        String[] temp = new String[arr.length];
        for (int i = 1; i < arr.length ; i++) {
            if(arr[i].length() > max) {
                max = arr[i].length();
                s = arr[i];
            }
        }
        for (int j = 0; j < arr.length ; j++) {
            if( arr[j].length() == s.length()){
                temp[j] = arr[j];
            }
        }

        return temp;
    }
}

 

Guess you like

Origin www.cnblogs.com/128-cdy/p/11839100.html