如何在每行有10个数字、每列有5个数字的情况下展示奇数?

huangapple go评论94阅读模式
英文:

How to display odd numbers in rows of 10 and columns of 5?

问题

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int number = 0;

    for (int row = 0; row <= 10; row++) {
        for (int column = 0; column <= 5; column++) {
            while (number < 100) {
                if (number % 2 == 1) {
                    System.out.print(number);
                }
                System.out.print(" ");
                number++;
            }
        }
    }
}

结果如下:

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 

如何将它们格式化为10行5列?

英文:
public static void main(String[] args) {
	// TODO Auto-generated method stub

	int number = 0;
	
	for (int row = 0; row &lt;=10; row++) {
		for (int column = 0; column &lt;= 5; column++) {
			while(number&lt;100) {
			    if (number%2 == 1) {
				    System.out.print(number);
			    }
			    System.out.print(&quot; &quot;);
			    number++;
			}
		}
	}
}

The result I get is :

> 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

How do I format them in 10 rows and 5 columns?

答案1

得分: 1

尝试这个:

for (int row = 0; row < 10; row++) {
    for (int column = 0; column < 10; column++) {
        if((row * 10 + column) % 2 != 0)
            System.out.print((row * 10 + column) + "\t");
    }
    System.out.print("\n");
}

编辑:

或者尝试这个更通用的解决方案,你只需要设置所需的行数和列数:

final int ROWS = 10;
final int COLUMNS = 5;

for (int row = 0; row < ROWS; row++) {
    for (int column = 0; column < COLUMNS * 2; column++) {
        if((row * COLUMNS * 2 + column) % 2 != 0)
            System.out.print((row * COLUMNS * 2 + column) + "\t");
    }
    System.out.print("\n");
}

或者像这样使用 while 循环:

final int ROWS = 10;
final int COLUMNS = 5;

int row = 0;
while(row < ROWS) {
    int column = 0;
    while(column < COLUMNS * 2) {

        if((row * COLUMNS * 2 + column) % 2 != 0)
            System.out.print((row * COLUMNS * 2 + column) + "\t");

         column++;
    }
    System.out.print("\n");
    row++;
}
英文:

Try this:

for (int row = 0; row &lt; 10; row++) {
    for (int column = 0; column &lt; 10; column++) {
        if((row * 10 + column) % 2 != 0)
            System.out.print((row * 10 + column) + &quot;\t&quot;);
    }
    System.out.print(&quot;\n&quot;);
}

EDIT:

Or try this more generic solution, you just have to set the amount of columns and rows you want:

final int ROWS = 10;
final int COLUMNS = 5;

for (int row = 0; row &lt; ROWS; row++) {
    for (int column = 0; column &lt; COLUMNS * 2; column++) {
        if((row * COLUMNS * 2 + column) % 2 != 0)
            System.out.print((row * COLUMNS * 2 + column) + &quot;\t&quot;);
    }
    System.out.print(&quot;\n&quot;);
}

Or use while loops like this:

final int ROWS = 10;
final int COLUMNS = 5;

int row = 0;
while(row &lt; ROWS) {
    int column = 0;
    while(column &lt; COLUMNS * 2) {

        if((row * COLUMNS * 2 + column) % 2 != 0)
            System.out.print((row * COLUMNS * 2 + column) + &quot;\t&quot;);

         column++;
    }
    System.out.print(&quot;\n&quot;);
    row++;
}

答案2

得分: 0

使用以下代码

        for (int row = 0; row <= 10; row++) {
            for (int column = 0; column <= 5; column++) {
                while (number < 100) {
                    if (number % 2 == 1) {
                        System.out.print(number);
                    }
                    System.out.print(" ");
                    number++;
                    if (number % 10 == 1) {
                        System.out.println("\n");
                    }
                }
            }
        }

输出:

    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
英文:

Use this:

    for (int row = 0; row &lt;=10; row++) {
        for (int column = 0; column &lt;= 5; column++) {
            while(number&lt;100) {
                if (number%2 == 1) {
                    System.out.print(number);
                }
                System.out.print(&quot; &quot;);
                number++;
                if (number % 10 ==1){
                    System.out.println(&quot;\n&quot;);
                }
            }
        }

    }

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 

答案3

得分: 0

你的两个 for 循环是无用的,它们没有执行任何操作,同时条件 number%2 == 1 不正确,用于检查奇数,它不能处理负数,但是我要祝贺你的尝试。

你会尝试吗?

for (int i = 0; i < 100; ++i) {
    if (i % 2 != 0) {
        System.out.print("  " + i + "  ");
    }
    if (i % 10 == 0)
        System.out.println();
}
英文:

your both for loops are useless they are doing nothing ,also the condition number%2 == 1 is incorrect for checking the odd number , it will not work for negative numbers , but i congratz you for trying .

will you try

 for(int i = 0 ; i &lt; 100 ; ++i) {
    	   if(i%2 != 0) {
    		   System.out.print(&quot;  &quot;+ i+ &quot; &quot;);
    	   }
    	   if(i % 10 == 0)
    		   System.out.println();
       }

答案4

得分: 0

请尝试以下代码。我不明白为什么你需要两个FOR循环。

int column = 0;
int number = 0;

while (number < 100) {
    if (number % 2 == 1) {
        System.out.print(number);
        column++;
    }
    if (column % 5 == 0) {
        System.out.print("\n");
    } else {
        System.out.print(" ");
    }
    number++;
}
英文:

Please try the below. I don't see why you need the two FOR loops.

int column = 0;
int number = 0;

while (number &lt; 100) {
	if (number % 2 == 1) {
		System.out.print(number);
		column++;
	}
	if (column % 5 == 0) {
		System.out.print(&quot;\n&quot;);
	} else {
		System.out.print(&quot; &quot;);
	}
	number++;
}

huangapple
  • 本文由 发表于 2020年5月3日 14:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61570440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定