[英]Java Programming for multiplication table of 2 only
Multiplication table for 2 only 乘法表仅2个
This is my code: 这是我的代码:
public class nest
{
public static void main (String[] args)
{
for (int row=1; row<=5; row++)
{
for (int column=1; column<=10; column++)
{
System.out.print(row*++column +"\t");
}
System.out.println();
}
}
}
This is my target results: 这是我的目标结果:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
But I'm getting this: 但是我得到这个:
2 4 6 8 10
4 8 12 16 20
6 12 18 24 30
8 16 24 32 40
10 20 30 40 50
Help me please I'm still learning Java. 请帮助我,我仍在学习Java。
The trick here is in being able to articulate a formula for the value in each cell given a row and column index. 这里的技巧是能够为给定行和列索引的每个单元格阐明一个值的公式。 Assuming we count both row and column starting from zero, then the formula is:
假设我们从零开始计数行和列,则公式为:
(2 + 10*row + 2*column)
This formula says that, starting with an initial value of 2, we increase by 10 moving down a row, and we increase by 2 moving to the right of a column. 该公式表示,从初始值2开始,我们在一行中向下移动增加10,而在列的右侧增加2。 This leads to the following code:
这将导致以下代码:
for (int row=0; row < 5; ++row) {
for (int column=0; column < 5; column++) {
System.out.print((2 + 10*row + 2*column) + "\t");
}
System.out.println();
}
To make it simple you can do something like this: 为了简单起见,您可以执行以下操作:
public class A{
public static void main(String[] args){
int k=2;
for (int row=1; row<=5; row++)
{
for (int column=1; column<=5; column++)
{
System.out.print(k+"\t");
k=k+2;
}
System.out.println();
}
}
}
Similar to the above one. 与以上类似。 A simple one.
一个简单的。
public static void main(String[] args) {
int tableOf = 2;
int currIter = tableOf;
for(int i = 1; i <= 5 ; i++) {
currIter = tableOf * i;
for(int j = 1; j <= 5 ; j++) {
System.out.print((currIter * j) + "\t");
}
System.out.println();
}
}
You can achieve your results by using an extra variable. 您可以通过使用额外的变量来获得结果。
public class nest
{
public static void main (String[] args)
{ int table= 0;
for (int row=1; row<=5; row++)
{
for (int column=1; column<=5; column++)
{
table+=2;
System.out.print(table +"\t");
}
System.out.println();
}
}
}
the loops can be used to define rows and columns for printing table . 循环可用于定义打印表的行和列。 hope it helped you a little.
希望它对您有所帮助。
You could also try (assuming you're using Java 8+), a formula based on breaking lines every five elements using a lambda and a ternary we can do so in a single statement. 您也可以尝试(假设您使用的是Java 8+),该公式基于使用lambda和三元数每五个元素对一行进行折行,我们可以在一个语句中执行此操作。 Generate a range of 1 to 25, multiply each value in the range by 2, and then print it (and if it isn't a multiple of 5 a tab, otherwise a newline).
生成一个1到25的范围,将该范围内的每个值乘以2,然后打印出来(如果它不是5的倍数,则制表符,否则为换行符)。 Like,
喜欢,
IntStream.rangeClosed(1, 25).map(i -> i * 2)
.forEachOrdered(i -> System.out.print(i + ((i % 5 != 0) ? "\t" : "\n")));
Your java code is great! 您的Java代码很棒! It appears that you're just having a little bit of math logic problems.
看来您只是遇到了一些数学逻辑问题。
Lets take a look at the following: 让我们看一下以下内容:
1 2 3 4 5
1
2
3
4
5
These are your rows and columns as you itterate through them in your loops. 这些是在循环中触发时的行和列。
2 4 6 8 10
Should be your first line which it is! 应该是您的第一行!
12 14 16 18 20
Should be your second line. 应该是您的第二行。 Well lets take a look at your equation.
好吧,让我们来看看您的方程式。
row*++column
++column means column + 1 = 2 ++列表示列+ 1 = 2
1 * 2 is 2 1 * 2是2
we want 12 我们想要12
If we keep the range of the loops within 5 its much simpler. 如果我们将循环范围保持在5以内,则简单得多。
public class nest
{
public static void main (String[] args)
{
for (int row=1; row<=5; row++)
{
for (int column=1; column<=5; column++)
{
System.out.print(logic math stuff +"\t");
}
System.out.println();
}
}
}
Looking at the expected output the numbers are just 1 - 25 * 2 从预期输出看,数字仅为1-25 * 2
Lets try to print 1 - 25 first. 让我们尝试先打印1-25。
Column itterates 1 - 5 so the first row is just column. 列的值是1-5,所以第一行只是列。
System.out.print(column + "\t");
1 2 3 4 5
Easy enough. 很简单。 So now on the second row we need 6 7 8 9 10.
因此,现在在第二行中,我们需要6 7 8 9 10。
Column is still 1 - 5 but row is now 2 列仍然是1-5,但行现在是2
The first number is supposed to be 6 so how do we get there? 第一个数字应该是6,那么我们如何到达那里?
row = 2 column = 1 行= 2列= 1
Our column width is 5 so every itteration of row we have to add 5 * (row-1). 我们的列宽是5,所以行的每一个变化必须加上5 *(row-1)。
System.out.print((column + 5 * (row-1)) + "\t");
This prints 1 - 25 in 5 rows 这将在5行中打印1-25
Now double it! 现在加倍!
System.out.print((column + 5 * (row-1)) * 2 + "\t");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.