[英]2D array with user input
I want to create a 5 row 2 column int array and have the user input each value in the array. 我想创建一个5行2列的int数组,并让用户在数组中输入每个值。 I want to use StdIn for the input. 我想使用StdIn作为输入。 Why won't this work? 为什么不起作用? Please help! 请帮忙! Thanks. 谢谢。
This is my effort: 这是我的努力:
int [][] a = new int [5][2];
int i;
int j;
for( i = 0; i < 4; i++ );
{
for( j = 0; j < 2; j++ );
{
System.out.println( "Month number (e.g. August = 8)" );
int month = StdIn.readInt();
a[i][0] = month;
System.out.println( "Year number (e.g. 2007)" );
int year = StdIn.readInt();
a[i][1] = year;
}
}
You're already asking both values from the user, no need for the nested loop: 您已经在向用户询问这两个值,不需要嵌套循环:
int [][] a = new int [5][2];
for(int i = 0; i < 5; i++ )
{
System.out.println( "Month number (e.g. August = 8)" );
int month = StdIn.readInt();
a[i][0] = month;
System.out.println( "Year number (e.g. 2007)" );
int year = StdIn.readInt();
a[i][1] = year;
}
I've also removed the semicolon ;
我也删除了分号;
you had after the first for loop making it useless, and fixed the iteration to get to 4 ( you're looping [0..4) and you probably want [0..5) ). 在第一个for循环之后使它无用,然后将迭代固定为4(正在循环[0..4),并且可能需要[0..5))。
j
was removed since the nested loop was not needed and I've make i
local to the for loop. j
被删除,因为不需要嵌套循环,并且我使i
在for循环本地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.