英文:
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:
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("%s", "Please enter a positive max. number: ");
int maxnum = in.nextInt();
System.out.printf("\n");
while (maxnum < 0) {
System.out.printf("%s" + "\n" + "\n", "Please enter a positive max. number: ");
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");
}
}
}
答案1
得分: 1
如果你仔细观察这个模式,你会发现右下三角形中的索引符合条件 max_count <= row + column
。
因此相应地,你需要在内部循环中添加一个条件,只有当条件满足时才打印数字。
if(maxnum <= i+j) {
System.out.printf("%5d", i * j);
} else {
System.out.printf("%5s", " ");
}
英文:
If you carefully look at the pattern, you'll see the indices in the lower right triangle conforms to the condition max_count <= 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 <= i+j) {
System.out.printf("%5d", i * j);
} else {
System.out.printf("%5s", " ");
}
答案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 <= 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");
}
I hope it'll help you !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论