解析二維陣列
阿新 • • 發佈:2018-12-20
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
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); int row = 0; int rank = 0; for(int i=0;i<s.length();i++) { char o = s.charAt(i); if(o == ';') { System.out.println(); row++; rank = 0; } else if(o == ',') System.out.print(" "); else if(i>=1 && o>='0' && o<='9' && s.charAt(i-1)>='0' && s.charAt(i-1)<='9') System.out.print(o); else { System.out.print("d[" + row + "," + rank +"] = "+o); rank++; } } scan.close(); } }