请教给位高手 在java里面怎样给二维数组赋值 ?

import java.io.*;public class Test { public static void main(String[] args) throws IOException{ String s[][] = new String[3][2];BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));for(int i = 0 ; i < 3 ; i ++){ for(int j = 0 ; j < 2...
请教给位高手 在java里面怎样给二维数组赋值 ?
要用循环的话,就用System.in接收输入的参数作为数组的值.

package test;

import java.io.*;

public class Test {

public static void main(String[] args) throws IOException{

String s[][] = new String[3][2];

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

for(int i = 0 ; i < 3 ; i ++){

for(int j = 0 ; j < 2 ; j ++) {

System.out.println("请输入第" + i + "行" + "第" + j + "列的值");

s[i][j] = reader.readLine();

}

}

for(int i = 0 ; i < 3 ; i ++){

for(int j = 0 ; j < 2 ; j ++) {

System.out.print(s[i][j] + " ");

}

System.out.println();

}

}

}

以上代码是途简便,最好还是加上try{}catch(){}finally{} 最后关闭IO流.2011-04-19
你这个根本没有规律,如果只是三行两列固定的数据 你为什么要用for循环赋值,直接一个一个赋值不是可以了吗? 如果你的数组中第一维是有规律的,第二维是没有规律的,那你能用for 来做吗? 所以你的需求本来就是很奇怪,先把你想要的需求讲明白 再提问2011-04-19
mengvlog 阅读 611 次 更新于 2025-09-11 01:10:04 我来答关注问题0
檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部