英文:
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 <=10; row++) {
for (int column = 0; column <= 5; column++) {
while(number<100) {
if (number%2 == 1) {
System.out.print(number);
}
System.out.print(" ");
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 < 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");
}
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 < 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");
}
Or use while
loops like this:
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++;
}
答案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 <=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");
}
}
}
}
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 < 100 ; ++i) {
if(i%2 != 0) {
System.out.print(" "+ i+ " ");
}
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 < 100) {
if (number % 2 == 1) {
System.out.print(number);
column++;
}
if (column % 5 == 0) {
System.out.print("\n");
} else {
System.out.print(" ");
}
number++;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论