简体   繁体   English

Java使用for循环产生一系列数字

[英]Java using for-loop to produce series of numbers

I am trying to write a collection of for-loops that produce the following series of numbers below. 我正在尝试编写一组for循环,这些循环在下面产生以下一系列数字。 I am trying to accommodate my loops to print each series on the same line, with spaces between each term. 我试图容纳我的循环以在同一行上打印每个系列,每个术语之间留有空格。 I am new to java and got really confused on how exactly I can accomplish it. 我是Java的新手,我对如何完全实现它感到非常困惑。 On the right side are the digits I am increasing the counting by. 右边是我要增加计数的数字。

 1. 4  5  6  7  8  9  10                (+1)
 2. 6  5  4  3  2  1                    (-1)
 3. 2  4  6  8  10  12  14  16          (+2)  
 4. 19  17  15  13  11  9  7  5         (-2)
 5. 7  15  23  31  39                   (+8)
 6. 2  4  8  16  32  64                 (*2)

Here is the code the way I tried to accomplish it. 这是我尝试完成此操作的代码。 I got the first row to work but I'm wondering weather there's an easy way I can create the rest of them without re-duplicating the program. 我有第一行可以工作,但是我想知道天气有一种简便的方法可以创建其余的程序,而无需重新复制程序。

import acm.program.*;

public class ppLoop extends ConsoleProgram {
    public void run()
    {
        {

        for(int row = 1; row < 2; row++)
        {
        print(" " + row + ". ");

        for (int col = 4; col < 11; col++)

            {
            print(row*col + " ");
            } //col values

            println( );
            } //row values

        }

    }
}

I am new to java and right now going over for-loops and trying to accomplish this in for-loop. 我是Java新手,现在要遍历for循环并尝试在for循环中完成此操作。 If someone could help me out, I would really appreciate it. 如果有人可以帮助我,我将非常感激。 Thank you! 谢谢! Edit: Here is what happens when I increase the number of rows. 编辑:这是当我增加行数时发生的情况。

在此处输入图片说明

Edit: Here is the solution of what I had tried accomplishing. 编辑:这是我尝试完成的解决方案。 Thanks to everyone who helped me. 感谢所有帮助我的人。

import acm.program.*;
public class ppLoop extends ConsoleProgram
{
    public void run()
    {
        {
        for(int i = 1; i < 2; i++) // One iteration of outer loop
             {
               print(i + ". "); // print row number 1

                for (int j = 4; j < 11; j++) // loop for row 1   
                {
                   print(j + " ");
                } 
                println( );
                print((i + 1) + ". ");

                for (int j = 6; j > 0; j--) // loop for row 2
                {
                   print(j + " ");
                }
                println();
                print((i + 2) + ". ");

                for (int j = 2; j < 17; j = j + 2) // loop for row 3
                {
                   print(j + " ");
                }
                 println();
                print((i + 3) + ". ");

                for (int j = 19; j > 4; j = j - 2) // loop for row 4
                {
                  print(j + " ");
                }
                 println();
                print((i + 4) + ". ");

                for (int j = 7; j < 40; j = j + 8) // loop for row 5
                {
                  print(j + " ");
                }
                println();
                print((i + 5) + ". ");

                for (int j = 2; j < 65; j = j * 2) // loop for row 6
                {
                print(j + " ");
                }
                println();
             } 
        } //close outer loop
    } //close public run
} //close console program

You can perform this program with a series of nested loops. 您可以使用一系列嵌套循环来执行此程序。 I have done the first three rows. 我已经完成了前三行。 I took out your package and used a main method. 我拿出您的包裹,并使用了一种主要方法。 Also, your indentation was very confusing. 另外,您的缩进非常令人困惑。 Since your increment changes each line, I don't know of a way to make it any shorter than this using for loops. 由于您的增量会改变每一行,因此我不知道一种使用for循环使其比此更短的方法。

public class ppLoop{
   public static void main(String[] args)
   {
      {

         for(int i = 1; i < 2; i++) // One iteration of outer loop
         {
            System.out.print(i + ". "); // print row number

            // you can use the same variable for each inner loop    
            for (int j = 4; j < 11; j++) // loop for row 1   
            {
               System.out.print(j + " ");
            } 

            System.out.println( );
            System.out.print((i + 1) + ". ");
            for (int j = 6; j > 0; j--) // loop for row 2
            {
               System.out.print(j + " ");
            }

            System.out.println();
            System.out.print((i + 2) + ". ");
            for (int j = 2; j < 17; j = j + 2) // loop for row 3
            {
               System.out.print(j + " ");
            }
         } 
      }
   }
}

You could create a method that takes: 您可以创建一个采用以下方法的方法:
1. A start number 1.起始号码
2. What math operation to perform (add, subtract, or multiply) 2.要执行的数学运算(加,减或乘)
3. What number to increment/decrement or multiply by 3.要递增/递减或乘以的数字
4. An end number 4.结束号码

It would look similar to this: 它看起来类似于:

public void formattedFor(int startNum, String operation, int num, int endNum) {
    if (operation.equals("add")) {
        for (int i = startNum; i < endNum; i += num) {
            System.out.print(i + " ");
        }
    }
    if (operation.equals("sub")) {
        for (int i = startNum; i > endNum; i -= num) {
            System.out.print(i + " ");
        }
    }
    else if (operation.equals("mult")) {
        for (int i = startNum; i < endNum; i *= num) {
            System.out.print(i + " ");
        }  
    }
    System.out.println( );   
}

If I'm understanding the problem, you want to print six series that each start with a different number and increment/decrement that number by some value. 如果我了解问题所在,则要打印六个系列,每个系列以不同的数字开头,然后以某个值递增/递减该数字。 Since I see no relationship between the initial value and the increment/decrement, you're going to have to write six separate for loops. 由于我看不到初始值与增量/减量之间的关系,因此您将不得不编写六个单独的for循环。

If you're absolutely averse to this, you can store your initial values, your increments/decrements, and your final values in an array and iterate through them using a for loop, an if statement (to deal with the multiplication) and a while loop. 如果您绝对不喜欢此方法,则可以将初始值,增量/减量和最终值存储在数组中,并使用for循环,if语句(以处理乘法)和一会儿遍历它们环。 The array would look like this: 该数组如下所示:

int[][] values = new int[][] {
        {4, 6, 2, 19, 7, 2},
        {1, -1, 2, -2, 8, 2},
        {10, 1, 16, 5, 39, 64}
    };

I could write up the source based on this, but it's not what you asked for. 我可以根据此来编写源代码,但这不是您要的。

I strongly suspect that, if this is a homework assignment and you've modified the problem, there's something you've failed to understand about the problem itself. 我强烈怀疑,如果这是一项家庭作业,并且您已经修改了问题,那么您可能无法理解问题本身。 If this is meant to have an simple solution that uses for loops, there should probably be some logic that binds the rows together, unless you're allowed to use arrays/while loops/for loops/objects and methods. 如果这是为了提供一种使用for循环的简单解决方案,则可能应该有一些将行绑定在一起的逻辑,除非允许您使用数组/ while循环/ for循环/对象和方法。

On another note, you should format your code differently. 另外,您应该以不同的方式设置代码格式。 It's somewhat difficult to read right now. 现在很难读。 In general, indent things that happen inside loops, classes, or functions. 通常,缩进在循环,类或函数内部发生的事情。 For example: 例如:

import acm.program.*;

public class ppLoop extends ConsoleProgram {
    public void run() {
        for(int row = 1; row < 2; row++) {
            print(" " + row + ". ");

            for (int col = 4; col < 11; col++) {
                print(row*col + " ");
            } //col values

            println( );
        } //row values
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM