Java 正确格式
有人告诉我,这段代码的格式设置不正确,有人能告诉我有什么问题吗Java 正确格式,java,for-loop,formatting,Java,For Loop,Formatting,有人告诉我,这段代码的格式设置不正确,有人能告诉我有什么问题吗 public class TwoDimArray { public static void main(String[] args) { int rows = 2; Int columns = 2; String[][] anArray = {{"Ireland", "Green"},{"England", "White"}}; for (int i = 0;
public class TwoDimArray {
public static void main(String[] args) {
int rows = 2;
Int columns = 2;
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}};
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.println(anArray[i][j]);
}
}
}
}
您编写的代码有一个错误:
Int columns = 2;
这将是正确的代码:
int columns = 2;
如果您希望您的代码具有以下输出:
爱尔兰绿
英格兰白人
您可以使用以下代码:
public class TwoDimArray {
public static void main(String[] args) {
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}};
for (int i = 0; i < anArray.length; i++){
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " ");
}
System.out.println();
}
}
}
公共类TwoDimArray{
公共静态void main(字符串[]args){
字符串[][]anArray={{“爱尔兰”、“绿色”}、{“英格兰”、“白色”};
for(int i=0;i
我希望能理解并帮助你
好运气公共类TwoDimArray{
public class TwoDimArray {
public static void main(String[] args) {
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}}; // create your array 2 dimensions
for (int i = 0; i < anArray.length; i++){ // create for, .length is the array size,
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " "); // print the element of array and add one space for print with you want
}
System.out.println(); //when end the FOR with J variable, you has printed the row, and with println, add a one new line
}
}
}
公共静态void main(字符串[]args){
字符串[][]anArray={{“爱尔兰”,“绿色”},{“英格兰”,“白色”};//创建数组2维
对于(int i=0;i
使用.length可以获得可变的大小/长度,在本例中,数组是数组[2][2]
我使用了一个println,因为当println结束时,这个方法在结束行中引入一个新行,一行跳转,下一次println写入将在新行中
但是,打印,与C语言中的打印相同,只写,下一次打印将在第一次打印结束时进行。。。因此,我在末尾打印中输入+”
for(int i=0;i
如果您仍然不理解,您可以尝试将“”更改为“”,或者尝试将println更改为print,您将开始理解
我希望现在能帮助你
幸运的是查看
System.out.print
vs.System.out.println
。基本上格式是可以的。然而,通常的标准是每个“嵌套级别”只缩进4个空格,关于{
是否应该遵循if
/的语句在同一行上还是在自己的行上,存在着一场宗教之争。(我倾向于将其放在if
/for
行中,以使列表更紧凑、更易于阅读。)而且符号之间的空格等方面存在一些小的风格问题(当然,一些#$&刚刚出现并“修复”了您的格式!)我看不出格式有任何问题。硬编码行
和列
并不是真正合适的编码(即,如果我们更改了数组而不是那些值,程序将无法正常工作)。你应该根据anArray
来检查长度。谢谢大家的帮助。我是Java新手,所以我只是想了解一下。我会研究一下。谢谢!谢谢你的帮助,但我不理解.length位。还有,为什么会有额外的系统.out.println();加入?对不起,我对Java还很陌生。
public class TwoDimArray {
public static void main(String[] args) {
String[][] anArray = {{"Ireland", "Green"},{"England", "White"}}; // create your array 2 dimensions
for (int i = 0; i < anArray.length; i++){ // create for, .length is the array size,
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " "); // print the element of array and add one space for print with you want
}
System.out.println(); //when end the FOR with J variable, you has printed the row, and with println, add a one new line
}
}
}
for (int i = 0; i < anArray.length; i++){
for (int j = 0; j < anArray.length; j++){
System.out.print(anArray[i][j] + " ");// First iteration of the FOR :Ireland" " second iteration of the FOR : Green" "
}
System.out.println(); // add a line jump for the next FOR iteration...
}
}