英文:
How to format multiplication table into 4 rows and columns respectively and test for even number multiplication table between 1 and 9?
问题
这是我根据外部视频教程尝试的代码,但在格式方面没有得到预期的输出。
public class Lab3Class {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int table = 1;
        while(table < 10) {
            int i = 1;
            while(i <= 10)
            {
                System.out.println(table + " * " + i + " = " + (table * i));
                i++;
            }
            System.out.println("  ");
            table++;
        }
    }
}
英文:
This is the code I atempted with the guide of external video which didnt cover expected output in terms of formatting
  public class Lab3Class {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    	int table = 1;
    	
    	while(table<10) {
    		 int i = 1;
    		 while(i<=10)
    		 {
    			 System.out.println(table+ " * "+i+" = "+(table*i));
    			 i++;
    		 }
    		System.out.println("  ");
    		table++;
    	}
    	
    }
    } 
答案1
得分: 0
你只是缺少了对偶数的检查,即 `if (table % 2 == 0)`。
public class Main {
    public static void main(String[] args) {
        int table = 1;
        while (table < 10) {
            if (table % 2 == 0) {
                int i = 1;
                while (i <= 10) {
                    System.out.println(table + " * " + i + " = " + (table * i));
                    i++;
                }
            }
            System.out.println();
            table++;
        }
    }
}
或者,你可以从 2 开始,并在每次迭代中将其增加 2,如下所示:
public class Main {
    public static void main(String[] args) {
        int table = 2;
        while (table < 10) {
            int i = 1;
            while (i <= 10) {
                System.out.println(table + " * " + i + " = " + (table * i));
                i++;
            }
            System.out.println();
            table += 2;
        }
    }
}
如果你需要以表格形式打印输出,可以按以下方式编写循环:
public class Main {
    public static void main(String[] args) {
        for (int line = 1; line <= 10; line++) {
            for (int i = 2; i <= 10; i += 2) {
                System.out.print(i + "*" + line + "=" + (i * line) + "\t");
            }
            System.out.println();
        }
    }
}
输出结果:
2*1=2    4*1=4    6*1=6    8*1=8    10*1=10    
2*2=4    4*2=8    6*2=12    8*2=16    10*2=20    
2*3=6    4*3=12   6*3=18   8*3=24   10*3=30    
2*4=8    4*4=16   6*4=24   8*4=32   10*4=40    
2*5=10   4*5=20   6*5=30   8*5=40   10*5=50    
2*6=12   4*6=24   6*6=36   8*6=48   10*6=60    
2*7=14   4*7=28   6*7=42   8*7=56   10*7=70    
2*8=16   4*8=32   6*8=48   8*8=64   10*8=80    
2*9=18   4*9=36   6*9=54   8*9=72   10*9=90    
2*10=20  4*10=40  6*10=60  8*10=80  10*10=100    
如你所见,使用 for 循环的方式看起来更加清晰。不过,我建议你也练习使用 while 循环。一旦你更加自信,我还建议你使用 String::format 或 System.out.printf 进行更好的格式化。
这是一个非常小的数据集,但如果数据集很大,你可以通过减少 I/O 操作来提高性能。为此,你可以将结果附加到 StringBuilder 并在最后一次性打印出来。
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int line = 1; line <= 10; line++) {
            for (int i = 2; i <= 10; i += 2) {
                sb.append(i).append('*').append(line).append('=').append(i * line).append('\t');
            }
            sb.append('\n');
        }
        System.out.println(sb);
    }
}
<details>
<summary>英文:</summary>
You are just missing a check for even numbers i.e. `if (table % 2 == 0)`.
    public class Main {
    	public static void main(String[] args) {
    		int table = 1;
    
    		while (table < 10) {
    			if (table % 2 == 0) {
    				int i = 1;
    				while (i <= 10) {
    					System.out.println(table + " * " + i + " = " + (table * i));
    					i++;
    				}
    			}
    			System.out.println();
    			table++;
    		}
    	}
    }
**Alternatively,** you can start table with `2` and increment it by 2 in each iteration as follows:
    public class Main {
    	public static void main(String[] args) {
    		int table = 2;
    
    		while (table < 10) {
    			int i = 1;
    			while (i <= 10) {
    				System.out.println(table + " * " + i + " = " + (table * i));
    				i++;
    			}
    			System.out.println();
    			table += 2;
    		}		
    	}
    }
If you need to print it in a tabular structure, you can write the loops as follows:
    public class Main {
    	public static void main(String[] args) {
    		for (int line = 1; line <= 10; line++) {
    			for (int i = 2; i <= 10; i += 2) {
    				System.out.print(i + "*" + line + "=" + (i * line) + "\t");
    			}
    			System.out.println();
    		}
    	}
    }
**Output:**
    2*1=2	4*1=4	6*1=6	8*1=8	10*1=10	
    2*2=4	4*2=8	6*2=12	8*2=16	10*2=20	
    2*3=6	4*3=12	6*3=18	8*3=24	10*3=30	
    2*4=8	4*4=16	6*4=24	8*4=32	10*4=40	
    2*5=10	4*5=20	6*5=30	8*5=40	10*5=50	
    2*6=12	4*6=24	6*6=36	8*6=48	10*6=60	
    2*7=14	4*7=28	6*7=42	8*7=56	10*7=70	
    2*8=16	4*8=32	6*8=48	8*8=64	10*8=80	
    2*9=18	4*9=36	6*9=54	8*9=72	10*9=90	
    2*10=20	4*10=40	6*10=60	8*10=80	10*10=100	
As you can see, it looks cleaner by using a `for` loop. However, I recommend you also practice it with a `while` loop. Once you gain more confidence, I also recommend you use [String::format][1] or `System.out.printf` for better formatting.
This is a very small data set but if the dataset is huge, you can improve the performance by reducing the I/O operation. For this, you can append the result to a `StringBuilder` and print it just once at the end.
    public class Main {
    	public static void main(String[] args) {
    		StringBuilder sb = new StringBuilder();
    		for (int line = 1; line <= 10; line++) {
    			for (int i = 2; i <= 10; i += 2) {
    				sb.append(i).append('*').append(line).append('=').append(i * line).append('\t');
    			}
    			sb.append('\n');
    		}
    		System.out.println(sb);
    	}
    }
  [1]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论