山科java實驗3-3編寫一個方法,返回一個double型的二維陣列,陣列中的元素通過解析字串引數獲得。例如,字串引數:“1,2;3,4,5;6,7,8”,對應的陣列為: d[0,0
阿新 • • 發佈:2018-12-19
編寫一個方法,返回一個double型的二維陣列,陣列中的元素通過解析字串引數獲得。例如,字串引數:“1,2;3,4,5;6,7,8”,對應的陣列為:
d[0,0] = 1.0 d[0,1] = 2.0
d[1,0] = 3.0 d[1,1] = 4.0 d[1,2] = 5.0
d[2,0] = 6.0 d[2,1] = 7.0 d[2,2] = 8.0
package 作業3; public class Five { public static void fun(String str){ double [][]a; String[] s1 = str.split(";"); a = new double[s1.length][]; for(int i=0; i<s1.length; i++){ String[] s2 = s1[i].split(","); a[i] = new double[s2.length]; for(int j=0; j<s2.length; j++){ a[i][j] = Integer.valueOf(s2[j]); } } for(double[] i:a){ for(double j:i){ System.out.print(j+" "); } System.out.println(); } } }
package 作業3;
//import java.util.*;
import java.util.Scanner;
import 作業3.Five;
public class Main {
public static void main(String[] args) {
String ss = "";
Scanner scanner = new Scanner(System.in);
ss = scanner.nextLine();
Five.fun(ss);
scanner.close();
}
}