使用嵌套 For 循环在 Java 中显示 1 - 99 之间的所有奇数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26608653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying all the Odd number between 1 - 99 in java using Nested For Loop
提问by Luke
Currently I'm only able to display odd numbers of 1 3 5 7 9. However, I would like to display all the odd numbers from 1 - 99 with 9 rows and 5 col. May I know how am I able to display from 11 onwards rather than just 9 rows of 1 3 5 7 9.
目前我只能显示 1 3 5 7 9 的奇数。但是,我想显示 1 - 99 之间的所有奇数,有 9 行和 5 列。我可以知道我如何能够从 11 开始显示,而不仅仅是 1 3 5 7 9 的 9 行。
Below is the code I'm stuck with.
下面是我坚持的代码。
public static void main(String args[])
{
for (int i=1; i<=9; i++)
{
for (int j=1; j<=10; j++)
{
if (j%2 !=0)
System.out.print(j + " " );
}
System.out.println();
}
}
采纳答案by user902383
first you need to calculate your number, try
首先你需要计算你的数字,试试
for (int i=0; i<=9; i++)
{
for (int j=1; j<=10; j++)
{
int number = j+i*10
if (number%2 !=0)
System.out.print(number + " " );
}
System.out.println();
}
but this problem you can solve with single loop
但是这个问题你可以用单循环解决
for (int i=1; i<=99; i++)
{
if (number%2 !=0)
System.out.print(number + " " );
if (number%10 ==0)
System.out.println();
}
回答by DirkNM
Maybe this helps:
也许这有帮助:
public static void main(String args[])
{
for (int j = 0; j < 10; j++)
{
for (int i = 1; i <= 9; i++)
{
int c = j * 10 + i;
if (c % 2 !=0)
System.out.print(c + " " );
}
System.out.println();
}
}
Other alternative with just one loop:
仅一个循环的另一种选择:
public static void main(String args[]) {
for (int j = 1; j <= 99; j += 2) {
System.out.print(j + " ");
if ((j + 1) % 10 == 0) {
System.out.println();
}
}
}
回答by Maroun
Your code now prints 9 rows of:
您的代码现在打印 9 行:
1 3 5 7 9
This is happening because your inner loop loops on values between 1 and 9, always.
发生这种情况是因为您的内部循环始终在 1 到 9 之间的值上循环。
You should try something like:
你应该尝试这样的事情:
int counter = 0;
for(int i=1; i<=99; i++) {
if(i%2 != 0) {
System.out.print(i + " ");
counter++;
}
if(counter == 5) {
System.out.println();
counter = 0;
}
}
This will print:
这将打印:
1 3 5 7 9
11 13 15 17 19
21 23 25 27 29
31 33 35 37 39
41 43 45 47 49
51 53 55 57 59
61 63 65 67 69
71 73 75 77 79
81 83 85 87 89
91 93 95 97 99
回答by brso05
public static void main(String args[])
{
for (int i=1; i<=99; i++)
{
if (i%2 !=0)
System.out.print(i + " " );
if(i%10 == 0)
System.out.println();
}
}
This code will allow you to do what you want with 1 loop which is more efficient than using nested loops.
此代码将允许您使用 1 个循环执行您想要的操作,这比使用嵌套循环更有效。
This will loop through each number from 1-99 and print if it is odd. If the number is a multiple of 10 then it will print a new line.
这将遍历从 1 到 99 的每个数字,如果它是奇数则打印。如果数字是 10 的倍数,那么它将打印一个新行。
回答by RealSkeptic
for ( i = 1; i < 100; i+=2 ) {
System.out.print(i);
}
System.out.println();
回答by vkluge
If you want to use a nested forloop you have to use both iterating variables to build your numbers. You dont use i at all in the inner for-loop. Therefore only 1 3 5 7 9 gets printed 9 times. For a hotfix try to use something like
如果要使用嵌套的 forloop,则必须同时使用两个迭代变量来构建数字。在内部 for 循环中根本不使用 i 。因此只有 1 3 5 7 9 被打印 9 次。对于修补程序尝试使用类似的东西
System.out.print(i + j + " " );
System.out.print(j + " " );
回答by Kent
we cheat:
我们作弊:
for (int i = 1; i <= 99; i += 2)
System.out.printf("%d%s", i, i % 10 == 9 ? "\n" : " ");
we check:
我们检查:
int c =0;
for (int i = 1; i <= 99; i++)
if ((i & 1) == 1) {
c++;
System.out.printf("%d%s", i, c % 5==0 ? "\n" : " ");
}
both output same, 5 odd numbers in a row, without trailing spaces.
两者输出相同,连续 5 个奇数,尾随空格。
回答by Olavi Mustanoja
With this one you can easily set the number of columns as you like it.
有了这个,您可以根据需要轻松设置列数。
We use a single loop.
我们使用一个循环。
Code:
代码:
int columns = 5;
int start = 1;
int end = 99;
// iterate through every seconds i.
for (int i = start; i <= end; i += 2) {
System.out.printf("%-4d", i);
// if we have displayed enough words, start a new line
if (i % (2 * columns) == 0) {
System.out.println();
}
}
Output:
输出:
1 3 5 7 9
11 13 15 17 19
21 23 25 27 29
31 33 35 37 39
41 43 45 47 49
51 53 55 57 59
61 63 65 67 69
71 73 75 77 79
81 83 85 87 89
91 93 95 97 99
However, if I understood your question correctly, you wanted to show numbers with 5 columns and 9 rows? Well this is impossible if we print only the numbers from 1 to 99. With 5 columns and numbers 1 to 99, you will get 10 rows.
但是,如果我正确理解了您的问题,您想显示 5 列 9 行的数字吗?好吧,如果我们只打印从 1 到 99 的数字,这是不可能的。如果有 5 列和数字 1 到 99,您将得到 10 行。