164 - 解析二维数组

164 - 解析二维数组

Time Limit: 1000   Memory Limit: 65535
Submit: 795  Solved: 221

Description

读入一个字符串,该字符串表示一个整型二维数组d,数组中的元素通过解析字符串参数获得。例如,字符串参数:“1,2;3,4,5;6,7,8”,对应的数组为:
d[0,0] = 1 d[0,1] = 2  
d[1,0] = 3 d[1,1] = 4 d[1,2] = 5
d[2,0] = 6 d[2,1] = 7 d[2,2] = 8
打印这个数组各元素的内容

Input

字符串

Output

二维数组各元素

Sample Input

1,2;3,4,5;6,7,8

Sample Output

d[0,0] = 1 d[0,1] = 2
d[1,0] = 3 d[1,1] = 4 d[1,2] = 5
d[2,0] = 6 d[2,1] = 7 d[2,2] = 8

HINT

 

Pre Append Code

Post Append Code

 

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String s = scan.nextLine();

        String [] str1 = s.split(";");//将字符串中以;为分隔符分隔为str1数组

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

        {

            String[] str2 = str1[i].split(",");//将字符串中以,为分隔符分隔为str2数组

            for(int j = 0; j < str2.length; j++)

            {

                if(j == 0)

                System.out.print("d["+i+","+j+"] = " + str2[j]);

                

                else

                    

                System.out.print(' '+"d["+i+","+j+"] = " + str2[j]);

            }

            System.out.print('\n');

        }

    }

}

 

你可能感兴趣的:(164 - 解析二维数组)