[英]Java 2D array with input and output
I'm trying to saving array with input some String type and to print it.我正在尝试使用输入一些字符串类型来保存数组并打印它。 But cannot understand why there is a error came up.. please advise me for any reasons.. Much appreciate in advance..
但不明白为什么会出现错误..请以任何理由告诉我..提前非常感谢..
static String Create(){
Scanner sc = new Scanner(System.in);
int num;
String rn,am,rt;
System.out.print("Enter Quantity:");
num = sc.nextInt();
String[][] result1= new String[num][2];
for(int i=0;i<num;i++) {
System.out.printf("Enter Details [%d](Room Number , Type , Rate)with blank : \n", (i+1));
rn = sc.next();
rt = sc.next();
am = sc.next();
for(int j=0;j<3;j++) {
result1[i][0] = rn;
result1[i][1] = rt;
result1[i][2] = am;
}
}
System.out.println();
System.out.println("---------------------------------");
System.out.printf("Total: %d\n", num);
System.out.println("---------------------------------");
System.out.println("Room Number\tRoom Type\tRate");
for(int f=0;f<num;f++){
for(int t=0;t<3;t++) {
System.out.println(result1);
}
}
return result1;
}
} }
j from below loop is not used, not sure what is the point of this loop: j from below loop 没有被使用,不知道这个循环的意义是什么:
for(int j=0;j<3;j++) {
At the end you try to display entire array instead of its elements.最后,您尝试显示整个数组而不是其元素。
It should be (with condition on t changed from 3 to 2:它应该是(条件从 3 变为 2:
for(int f=0;f<num;f++){
for(int t=0;t<2;t++) {
System.out.println(result1[f][t]);
}
}
You also return an array at the end while the method is defined to return a string.当方法被定义为返回一个字符串时,您还可以在最后返回一个数组。 This might be the error you are getting.
这可能是您遇到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.