删除乘法表中的左上三角形。

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

Deleting upper left triangle from multiplication table

问题

我是编程新手,正在学习Java编程入门课程。

我有一个作业,但似乎无法完成要求的最后部分。

我正在创建一个源代码,它接受一个输入值a,并创建一个a x a的乘法表格。

到目前为止,我有:

[图片描述见链接1]

最终产品应该看起来像图中的样子。我觉得下一步是添加一个新变量,在第一次迭代的末尾打印maxnum变量,并在该迭代的其余部分留出空格。但我在确定如何做这件事时遇到了困难。我觉得我需要另一个for语句,但不知道该放在哪里。我的初步猜测是在使用变量j的for语句之后,但运气不太好。任何建议将不胜感激,谢谢大家!

到目前为止,我的代码如下:

import java.util.Scanner;

public class MultiTable {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.printf("%s", "请输入一个正数最大值:");
    int maxnum = in.nextInt();
    System.out.printf("\n");
    while (maxnum < 0) {
      System.out.printf("%s" + "\n" + "\n", "请输入一个正数最大值:");
      maxnum = in.nextInt();
    }
    int i = 0;
    int j;
    int k;
    System.out.printf("%10d", i);
    for (i = 0; i <= maxnum - 1; i++) {
      System.out.printf("%5d", i + 1);
    }
    System.out.printf("\n" + "-----");
    for (i = 0; i <= maxnum; i++) {
      System.out.printf("-----");
    }
    System.out.printf("\n");
    for (i = 0; i <= maxnum; i++) {
      System.out.printf("%3d%2s", i, " |");
      for (j = 0; j <= maxnum; j++) {
        System.out.printf("%5d", i * j);
      }
      System.out.printf("\n");
    }
  }
}

[图片描述见链接2]

英文:

I'm new to programming and am taking an intro to programming class which focuses on java.

I have an assignment that have been working on but can't seem to get the last portion of the requirements.

I am creating a source code that takes an input value, a, and creates a multiplication table a x a.

So far I have:

enter image description here

The end product should look something like the image. I feel like the next step is adding a new variable that prints the maxnum variable in the end of the first iteration and leaves whitespace in front of the rest of that iteration. I'm having trouble determining how to do it though. I feel like I need another for statement but don't know where to place it. My initial guess is after the for statement that uses the variable j but haven't had much luck. Any suggestions would be appreciated, thank you all!

What I have so far:

import java.util.Scanner;

public class MultiTable {
  public static void main(String[] args) {
    Scanner in =new Scanner(System. in );
    System.out.printf(&quot;%s&quot;, &quot;Please enter a positive max. number: &quot;);
    int maxnum = in.nextInt();
    System.out.printf(&quot;\n&quot;);
    while (maxnum &lt; 0) {
      System.out.printf(&quot;%s&quot; + &quot;\n&quot; + &quot;\n&quot;, &quot;Please enter a positive max. number: &quot;);
      maxnum = in.nextInt();
    }
    int i = 0;
    int j;
    int k;
    System.out.printf(&quot;%10d&quot;, i);
    for (i = 0; i &lt;= maxnum - 1; i++) {
      System.out.printf(&quot;%5d&quot;, i + 1);
    }
    System.out.printf(&quot;\n&quot; + &quot;-----&quot;);
    for (i = 0; i &lt;= maxnum; i++) {
      System.out.printf(&quot;-----&quot;);
    }
    System.out.printf(&quot;\n&quot;);
    for (i = 0; i &lt;= maxnum; i++) {
      System.out.printf(&quot;%3d&quot; + &quot;%2s&quot;, i, &quot; |&quot;);
      for (j = 0; j &lt;= maxnum; j++) {
        System.out.printf(&quot;%5d&quot;, i * j);
      }
      System.out.printf(&quot;\n&quot;);
    }
  }
}

enter image description here

答案1

得分: 1

如果你仔细观察这个模式,你会发现右下三角形中的索引符合条件 max_count &lt;= row + column

因此相应地,你需要在内部循环中添加一个条件,只有当条件满足时才打印数字。

if(maxnum &lt;= i+j) {
	System.out.printf(&quot;%5d&quot;, i * j);
} else {
	System.out.printf(&quot;%5s&quot;, &quot; &quot;);
}
英文:

If you carefully look at the pattern, you'll see the indices in the lower right triangle conforms to the condition max_count &lt;= row + column.

So accordingly you'll need to add a condition in the inner loop to print the numbers only if the condition satisfies.

if(maxnum &lt;= i+j) {
	System.out.printf(&quot;%5d&quot;, i * j);
} else {
	System.out.printf(&quot;%5s&quot;, &quot; &quot;);
}

答案2

得分: 0

这是您尝试做的吗?乘法表三角形

对于这个结果,您可以使用两个 "for" 循环来替代您的最后一个循环 ^^:

for (int i = 0; i <= maxnum; i++){
    System.out.printf("%3d"+"%2s", i, " |");
    for (int j = 0; j < i; j++){
        System.out.printf(" ".repeat(5));
    }
    for (int j = i; j <= maxnum; j++){
        System.out.printf("%5d", i*j);
    }
    System.out.printf("\n"); 
}

希望这对您有所帮助!

英文:

Is that what you were trying to do ? multiplication table triangle

For this result, you can use two "for" loops in replacement of your last loop ^^ :

for (int i = 0; i &lt;= maxnum; i++){
System.out.printf(&quot;%3d&quot;+&quot;%2s&quot;, i, &quot; |&quot;);
for (int j = 0; j &lt; i; j++){
System.out.printf(&quot; &quot;.repeat(5));
}
for (int j = i; j &lt;= maxnum; j++){
System.out.printf(&quot;%5d&quot;, i*j);
}
System.out.printf(&quot;\n&quot;); 
}

I hope it'll help you !

huangapple
  • 本文由 发表于 2023年7月3日 16:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603127.html
匿名

发表评论

匿名网友

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

确定