帮助理解 java 'for' 循环 [英] Help with understanding java 'for' loops

查看:38
本文介绍了帮助理解 java 'for' 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to write a java program where the solution will include the printing of the arrow tip figure depending on the number of rows. Below are example of how the result should look. However, I cannot do this until I understand for loops. I know I have to work with the rows and columns and possibly nested loops. I just dont know how to connect the row with the columns using for loops. Please help me in understanding these loops. Thanks!

示例 #1(奇数行)

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

示例#2(偶数行)

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

推荐答案

for 循环将遍历数据集合,例如数组.经典的 for 循环如下所示:

a for loop will loop through a collection of data, such as an array. The classic for loop looks like this:

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

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

the first param is a counter variable. the second param expresses how long the loop should last, and the 3rd param expresses how much the counter should be incremented by after each pass.

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

if we want to loop from 1 - 10, we do the following:

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

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

if we want to loop from 10 - 1, we do the following:

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

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

if we want to loop through a 2 dimensional collection, like...

1 2 3
4 5 6
7 8 9

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

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

we need 2 loops. The outer loop will run through all the rows, and the inner loop will run through all the columns.

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

you are going to need 2 loops, one to iterate through the rows, one to iterate through the columns.

 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 来引用我们在循环中的位置.

In the outer loop, we set a counter to 0 for the first parameter. for the second, to calculate how many times we will loop, we use the length of the array, which will be 3, and for the third param, we increment by one. we can use the counter, i, to reference where we are inside the loop.

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

We then determine the length of the specific row by using grid[i].length. This will calculate the length of each row as they are being looped through.

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

Please feel free to ask any questions you may have regarding for loops!

理解问题.....

您将不得不对您的代码做几件事.这里我们将把行数存储在一个变量中,如果您需要将此值传递给一个方法,请说出来.

You are going to have to do several things with your code. Here we will store the number of lines in a variable, speak up if you need to pass in this value to a method.

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

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

以上将打印出一路上涨的克拉数.我们打印出克拉变量,然后我们使克拉变量长 2 克拉.

The above will print out carats going all the way up. We print out the carat variable then we make the carat variable 2 carats longer.

....接下来要做的事情是决定何时减少克拉数,或者我们可以增加一半,减少另一半.

.... the next thing to do is to implement something that will decide when to decrease the carats, or we can go up half of them and down the other half.

编辑 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 + "
");                           
                if(i==half && even){System.out.println(carat+"
");} // 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
                }
        }
    }
}

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

Explanation and commentary along shortly. Apologies if this is over complicated (i'm pretty sure this is not even close to the best way to solve this problem).

考虑这个问题,我们有一个驼峰,偶数出现在一半,奇数出现一半.

Thinking about the problem, we have a hump that appears halfway for even numbers, and halfway rounded up for the odd numbers.

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

At the hump, if it is even, we have to repeat the string.

然后我们必须开始起飞<<"每次,因为我们要下去了.

We have to then start taking off "<<" each time, since we are going down.

如果您有问题,请提问.

Please ask if you have questions.

这篇关于帮助理解 java 'for' 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆