[Stitching characters according to the specified format]

package cn.itcast.day08.demo02; 

/* 
Title: 
Define a method to concatenate the array {1, 2, 3} into a string according to the specified format. The format is as follows: [word1#word2#word3] 

Analysis: 
1. First prepare an int array, the content is 1, 2, 3 
2. Define a method to convert the array into a string 
Three elements 
return value type: String 
method Name: fromArrayToString 
Parameter list: int[] 
3. Format: [word1#word2#word3] 
Used: for loop, string splicing, there is a word before each array element, # is used for division, and the distinction is Not the last 
4. Call the method, get the return value, and print the result 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;
    }
}

Guess you like

Origin blog.csdn.net/m0_48114733/article/details/123301843