如何将乘法表格式化为4行和4列,然后测试1到9之间的偶数乘法表?

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

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&lt;10) {
    		 int i = 1;
    		 while(i&lt;=10)
    		 {
    			 System.out.println(table+ &quot; * &quot;+i+&quot; = &quot;+(table*i));
    			 i++;
    		 }
    		System.out.println(&quot;  &quot;);
    		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::formatSystem.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 &lt; 10) {
    			if (table % 2 == 0) {
    				int i = 1;
    				while (i &lt;= 10) {
    					System.out.println(table + &quot; * &quot; + i + &quot; = &quot; + (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 &lt; 10) {
    			int i = 1;
    			while (i &lt;= 10) {
    				System.out.println(table + &quot; * &quot; + i + &quot; = &quot; + (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 &lt;= 10; line++) {
    			for (int i = 2; i &lt;= 10; i += 2) {
    				System.out.print(i + &quot;*&quot; + line + &quot;=&quot; + (i * line) + &quot;\t&quot;);
    			}
    			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 &lt;= 10; line++) {
    			for (int i = 2; i &lt;= 10; i += 2) {
    				sb.append(i).append(&#39;*&#39;).append(line).append(&#39;=&#39;).append(i * line).append(&#39;\t&#39;);
    			}
    			sb.append(&#39;\n&#39;);
    		}
    		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>



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

发表评论

匿名网友

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

确定