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

问题描述

我必须写一个Java程序,其中解决方案将包括根据行数打印箭头提示图形。下面是结果应该如何看的例子。但是,我不能这样做,直到我理解循环。我知道我必须使用行和列以及可能的嵌套循环。我只是不知道如何使用for循环连接行的列。请帮我理解这些循环。谢谢!

示例#1(奇数行)

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

示例#2(偶数行)

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


解决方案

一个for循环会循环一个数据集合,如数组。经典for循环看起来像这样:

$ p $ for(counter = 0; counter

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



如果我们想从1 - 10,我们做如下:
$ b $ pre $ for(counter = 1; counter

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

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

如果我们想循环2维集合,比如...

  1 2 3 
4 5 6
7 8 9

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

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



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



pre $ for(i = 0; i< grid.length; i ++){
//这将循环遍历所有行...
for(j = 0; j< grid [i] .length; j ++){
//将遍历第一行中的所有列,然后在第二行的所有列,等等。
System.out.println('row'+ i +' - '+'column'+ j +':'+ grid [i] [j]);






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

然后我们用grid [i] .length来确定特定行的长度。这将计算每一行的长度,因为它们是循环的。



请随时询问您可能有关于循环的任何问题!



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

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

  int lines = 10; //行数
String carat =>; (i = 1; i System.out.println(carat +\\\
);

//最后一部分换行
carat = carat +>>;

$ / code $ / pre
$ b $ p

上面会打印出克拉。我们打印出克拉变量,然后使克拉变长2克拉。

...接下来要做的是实现一些决定何时减少克拉,或者我们可以上涨一半,下半年。

编辑3:

 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;} //如果是偶数,余数为0

for(i = 1; i System.out.println(carat +\\\
);
if(i == half& even){System.out.println(carat +\\\
);} //如果这是中间数字并且行数是(((i> = half& even)||(i> = half + 1))&& i!= lines){//如果数字是偶数并且等于或超过一半,或者如果是一半以上(对于奇数的输出),并且这不是通过循环的最后一次,那么在字符串
克拉=克拉.substring(0,carat.length() - 2);
} else {
carat = carat +>>; // b





















解释和评论很快。道歉,如果这是过于复杂(我敢肯定,这甚至不接近解决这个问题的最佳方式)。

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

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

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



请问是否有问题。


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!

Example #1 (odd number of rows)

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

Example #2 (even number of rows)

>
>>>
>>>>>
>>>>>>>
>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>
>>>>>>>
>>>>>
>>>
>
解决方案

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.

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

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

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}};

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

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]);
    }
 }

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.

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.

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

EDIT: understanding the question.....

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 + "\n"); // last part for a newline
     carat = carat + ">>";
 }

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.

Edit 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
                }
        }
    }
}

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'循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:30