你好,我有和赋值,它告诉我运行下面的java代码,但我一直收到3个类似的错误,比如变量studentInfo不能解析为一个变量
这是错误
错误: studentInfo不能解析为变量行11错误: studentInfo不能解析为变量行15错误: studentInfo不能解析为变量行15。
public class DisplayInfoExersice {
public static void main( String [ ] args ) {
int studentInfo [ ] [ ] = { {1, 78, 85}, {2, 83, 90} };
display( studentInfo );
}
public static void display( int array [ ] [ ] ) {
for ( int j = 0; j < studentInfo.length; j++ ) {
System.out.println( );
for ( int k = 0; k < studentInfo[j].length; k++)
System.out.print ( studentinfo [j] [k] + "\t"
);
}
System.out.println( );
}
}
请帮帮忙。
发布于 2018-04-29 00:42:27
//这是正确的版本。
public class DisplayInfoExcercise {
public static void main( String [ ] args )
{
int studentInfo [ ] [ ] = { {1, 78, 85}, {2, 83, 90} };
display( studentInfo );
}
public static void display( int array [ ] [ ] )
{
for ( int j = 0; j < array.length; j++ ) //
{
System.out.println( );
for ( int k = 0; k <array[j].length; k++)
System.out.print ( array[j] [k] + "\t" );
}
System.out.println( );
}
}
发布于 2018-04-29 00:38:18
您在array
方法声明中将其命名为display
。最简单的修复更改
public static void display( int array [ ] [ ] )
至
public static void display( int studentInfo [ ] [ ] )
您还可以用Java 8+重写整个程序,如
int[][] studentInfo = { { 1, 78, 85 }, { 2, 83, 90 } };
System.out.printf("%n%s%n",Stream.of(studentInfo) //
.flatMapToInt(IntStream::of) //
.mapToObj(String::valueOf) //
.collect(Collectors.joining("\t")));
发布于 2018-04-29 00:39:12
您正在使用在主函数上声明的局部变量studentInfo,在显示函数中。您将此变量作为参数传递。您需要使用该参数的名称:
for ( int j = 0; j < array.length; j++ ) //
for ( int k = 0; k < array[j].length; k++)
System.out.print ( array[j] [k] + "\t" );
https://stackoverflow.com/questions/50084750
复制相似问题