日期:2014-05-18 浏览次数:20841 次
import java.io.*;
public class Practice1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int a[][]=new int [4][4];
for(int k=0;k<4;k++)
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
for(int w=0;w<4;w++)
{
a[k][w]=Integer.parseInt(br.readLine());
}
}
for(int k=0;k<4;k++)
{
for(int w=0;w<4;w++)
{
System.out.print(a[k][w]+" ");
}
System.out.println();
}
}
}
我想一行一行的 给二维数组输入值 求大神帮助!
import java.io.*;
import java.util.Scanner;
public class Practice1 {
private int[][] array;
private int rows = 1;
private int cols = 1;
public void serviceB(){
Scanner scanner = new Scanner(System.in);
boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
System.out.println("请输入您要生成的矩阵行数:");
while(b1==false){
try{
String sRow = scanner.next();
rows = Integer.parseInt(sRow);
b1 = true;
}catch (Exception e) {
System.out.println("请输入一个数字");
}
}
System.out.println("请输入您要生成的矩阵列数:");
while(b2==false){
try{
String sCol = scanner.next();
cols = Integer.parseInt(sCol);
b2 = true;
}catch(Exception e){
System.out.println("请输入一个数字");
}
}
System.out.println("初始化" + rows + "行" + cols + "列数组" );
array = new int[rows][cols];
while(b3==false){
for(int i=0;i<rows;i++){
System.out.println("请输入第" + (i+1) + "行数据,格式为 num1,num2,num3,num4");
String nextLine = scanner.next();
String[] numString = nextLine.split(",");
if(numString.length==cols){
for(int j=0;j<cols;j++){
array[i][j] = Integer.parseInt(numString[j]);
}
b3=true;
}else{
System.out.println("输入的数组长度与列数不一致,请重新录入");
i--;
}
}
}
print(array);
}
public void print(int[][] arr){
System.out.println("-----生成二维数组-----");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Practice1 p = new Practice1();
p.serviceB();
}
}