Java learning - by the specified format mosaic character

/ *
Title:
define a method, the array {1,2,3} assembled into a string in the specified format. Reference to the following format: [word1 # word2 # word3] .

analysis:

  1. First prepare a int [] array content is: 1,2,3
  2. Define a method for the array into a string
    of three elements
    Return Value Type: String
    method name: fromArrayToString
    list of parameters: int []
  3. Format: [word1 # word2 # word3]
    use: for loop, string concatenation, the word has a word before each array element, using a # separate, distinguish between what is not the last
  4. Method call, return value, and prints the resulting string
    * /
public class Demo06StringPractise {
    public static void main(String[] args) {
        int[] array = {1, 2, 3};

        String result = fromArrayToString(array);
        System.out.println(result);
    }

    public static String fromArrayToString(int[] array) {
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            if (i == array.length-1){
                str += "word" +array[i] + "]";
            }else {
            str += "word" + array[i] + "#";
            }
        }
        return str;
    }
}
Published 23 original articles · won praise 0 · Views 154

Guess you like

Origin blog.csdn.net/qq_44813352/article/details/104315818