java - 帮助理解 java 'for' 循环

标签 java loops for-loop

我必须编写一个java程序,其中解决方案将包括根据行数打印箭头提示图。下面是结果的示例。但是,在我理解 for 循环之前我无法做到这一点。我知道我必须处理行和列以及可能的嵌套循环。我只是不知道如何使用 for 循环将行与列连接起来。请帮助我理解这些循环。谢谢!

示例#1(奇数行)

>
>>>
>>>>>
>>>>>>>
>>>>>
>>>
>

示例#2(偶数行)

>
>>>
>>>>>
>>>>>>>
>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>
>>>>>>>
>>>>>
>>>
>

最佳答案

for 循环将循环访问数据集合,例如数组。经典的 for 循环如下所示:

  for(counter=0;counter <= iterations;counter++){   }

第一个参数是一个计数器变量。第二个参数表示循环应持续多长时间,第三个参数表示每次循环后计数器应增加多少。

如果我们想从 1 到 10 循环,我们执行以下操作:

for(counter=1;counter<=10;counter++){ System.out.println(counter); }

如果我们想从 10 - 1 循环,我们执行以下操作:

for(counter=10;counter>=1;counter--){  System.out.println(counter); }

如果我们想循环遍历一个二维集合,比如......

1 2 3
4 5 6
7 8 9

int[][] grid = new int[][] {{1,2,3},{4,5,6},{7,8,9}};

我们需要 2 个循环。外循环将遍历所有行,内循环将遍历所有列。

您将需要 2 个循环,一个循环遍历行,一个循环遍历列。

 for(i=0;i<grid.length;i++){
    //this will loop through all rows...
    for(j=0;j<grid[i].length;j++){
      //will go through all the columns in the first row, then all the cols in the 2nd row,etc
      System.out.println('row ' + i + '-' + 'column' + j + ':' + grid[i][j]);
    }
 }

在外层循环中,我们将第一个参数的计数器设置为 0。对于第二个参数,为了计算我们将循环多少次,我们使用数组的长度,即 3,对于第三个参数,我们增加 1。我们可以使用计数器 i 来引用我们在循环内的位置。

然后我们使用 grid[i].length 确定特定行的长度。这将计算循环时每行的长度。

请随时提出有关 for 循环的任何问题!

编辑:理解问题......

您必须对代码执行几项操作。这里我们将行数存储在一个变量中,如果您需要将此值传递给方法,请大声说出来。

 int lines = 10; //the number of lines
 String carat = ">";

 for(i=1;i<=lines;i++){
     System.out.println(carat + "\n"); // last part for a newline
     carat = carat + ">>";
 }

以上将打印出一路向上的克拉数。我们打印出克拉变量,然后将克拉变量加长 2 克拉。

....接下来要做的事情是实现一些决定何时减少克拉的东西,或者我们可以增加一半并减少另一半。

编辑3:

Class Test {
    public static void main(String[] args) {

        int lines = 7; 

        int half = lines/2;
        boolean even = false;
        String carat = ">";
        int i;

        if(lines%2==0){even = true;} //if it is an even number, remainder will be 0

        for(i=1;i<=lines;i++){
                System.out.println(carat + "\n");                           
                if(i==half && even){System.out.println(carat+"\n");} // print the line again if this is the middle number and the number of lines is even
                if(((i>=half && even) || (i>=half+1)) && i!=lines){ // in english : if the number is even and equal to or over halfway, or if it is one more than halfway (for odd lined output), and this is not the last time through the loop, then lop 2 characters off the end of the string
                        carat = carat.substring(0,carat.length()-2); 
                }else{ 
                        carat = carat + ">>"; //otherwise, going up
                }
        }
    }
}

很快就会有解释和评论。如果这过于复杂,我深表歉意(我很确定这甚至不是解决此问题的最佳方法)。

考虑一下这个问题,我们有一个驼峰,对于偶数,它出现在中间,对于奇数,它出现在中间。

在驼峰处,如果是偶数,我们就必须重复该字符串。

每次我们都必须开始起飞“<<”,因为我们正在下降。

如有疑问请询问。

关于java - 帮助理解 java 'for' 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5162845/

相关文章:

jquery - 创建一个数组并循环显示每个 HTML

c++ - 'for loop' 的终止条件是否在 VC++ 6 中刷新?

java - 如何将对象绑定(bind)到 Swing 表中的一行?

java - 如何使用 return 语句从 Java 中的 for 循环返回多个值?

java - 如何实现循环输入

python - 如何对数据集中的某些单词进行值计数

python - 在 python 的 for 循环中将行写入 csv 文件

c# - 为数组中字符串的每个字母输出 _ (hangman)

java - 如何使用简单的适配器和 ListView 创建自己的自定义行布局

java - 在没有 persistence.XML 的情况下使用 JPA 和 Spring 连接到 mysql 数据库