如何在java中使用for循环打印数字?

时间:2012-10-27 04:56:23

标签: java loops for-loop printf

我有一个文本框,其中我取值how many number do you want to print?现在我的问题是,如何使用for循环以便我要打印的数字等于我从文本框中获得的数字?还有一件事是我想在一行中只打印三个数字。

即。如果我的文本框中有14,结果将如下所示。

1  2   3 

4  5   6

7  8   9

10 11  12

13 14

1 个答案:

答案 0 :(得分:0)

假设您从文本框中捕获了值并将其保存为数字:

这将打印出您指定的数字:

public void print_numbers(int num) {

      int counter = 1;

      while ( counter <= num ) {

          System.out.print(counter);
          if ( counter % 3 == 0 ) {
              System.out.print("\n");
          }
          counter++;
      }
}
相关问题