Find the shortest longest string

2019-11-11  22:18:09

Exercise 1 : Input n lines of character strings, which strings to find the longest, shortest string; if the same longest, shortest, the printed together.

Initially thought : the n-line string into an array. (Scanner s = new Scanner (System.in );)

Write method to find the longest string : 0 first element referred to as the length of the longest string. Record length of the current element number 0 is a max value. for looping through the rest of the string, if its length is greater than the max value is updated to the new length of the string, the string will be updated to the current string. The same method to find the shortest string.

If you have a long string of the same length, how do? The idea is to begin: the same string length may be more than one, so in addition to open up an array, the same length of the string into an array, and returns an array. Finally, no change is successful, the method also thought string concatenation; defining another string, if there are equal length, two strings will be spliced together, the splicing return string

private static String findMin(String[] arr) {
        String str4 = arr[0];
        //String str5 = arr[arr.length-1];
        int min = str4.length();
        for (int i = 1; i < arr.length; i++) {
            if (arr[i].length() < min) {
                min = arr[i].length();
                str4 = arr[i];
                for (int j = i+1; j < arr.length; j++) {
                    if (arr[j].length() == arr[i].length()) {
                        String str5= arr[j];
                        str4=str4+" "+str5;
                    }
                }
            }
        }
        return str4+" ";
    }

Initially, there may be additional strings defined in the beginning, but this string may not exist, so then it will be defined in the judge sentences inside; to achieve a string concatenation, the result of splicing return.

Another problem in the main function: after due to the number of input string, so to enter a number, then reminded the input string to compare, because the input format has changed, the compiler will default as a carriage return a string, you need a nexLine (), replace the Enter key. There are: the lack of input into the string array to compare to the inside of the statement (careless !!)

        Scanner s = new Scanner(System.in);
        System.out.println ( "Please enter a string of n" );
         int n = s.nextInt ();
        String[] arr = new String[n];
        s.nextLine();
        System.out.println ( "Please enter the string" );
         for ( int I = 0; I <n-; I ++ ) {
            arr[i] = s.nextLine();
        }

The entire code is:

import java.util.Scanner;

// input string of n rows, find the longest shortest string (if the same number of printing two) 
public  class the FindString {
     public  static  void main (String [] args) {
        Scanner s = new Scanner(System.in);
        System.out.println ( "Please enter a string of n" );
         int n = s.nextInt ();
        String[] arr = 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);
        System.out.println ( "Maximum string:" + max.toString ()); // String toString method has been rewritten, it will print the character string 
        String min = FindMin (ARR);
        System.out.println ( "minimal string:" + min.toString ());
    }

    private static String findMin(String[] arr) {
        String str4 = arr[0];
        //String str5 = arr[arr.length-1];
        int min = str4.length();
        for (int i = 1; i < arr.length; i++) {
            if (arr[i].length() < min) {
                min = arr[i].length();
                str4 = arr[i];
                for (int j = i+1; j < arr.length; j++) {
                    if (arr[j].length() == arr[i].length()) {
                        String str5= arr[j];
                        str4=str4+" "+str5;
                    }
                }
            }
        }
        return str4+" ";
    }
    private static String findMax(String[] arr) {
        Str String = ARR [0];   // the bit number 0 is defined as an array of length of the longest string str, sequentially comparing
         // String STR6 = ARR [arr.length -. 1];
         // String [] = new new Strr String [arr.length]; 
        int max = str.length ();
         for ( int I =. 1; I <arr.length; I ++ ) {
             IF (ARR [I] .length ()> max) {    // If a a length of the string it is large than 
                max = ARR [I] .length (); // the length of the string is updated maximum 
                str = ARR [I]; // and assigns the string str 
                for ( int J = I +. 1; J <arr.length; J ++) { // if the string of the same length
                    if (arr[j].length() == arr[i].length()) {
                        STR6 String = ARR [J];   // this string to a new str6, and then print it out with 
                        STR STR = + "" + STR6;
                    }
                }
            }
        }
        return str + " ";
    }

}

 

 

Guess you like

Origin www.cnblogs.com/laurarararararara/p/11839040.html