我有这个代码,它做了一个乘法表,但我需要它做的只是一个10乘法表,意思是10 x 1是10,10 x 2是20,10 x 3是30........etc to 10 x 10是100,这是我的代码,但我不知道如何编辑它。有谁可以帮我?
public class TimesTable {
public static void main(String[] args) {
int lowerValue = 1;
int upperValue = 10;
for (int headingIndex = lowerValue; headingIndex <= upperValue; headingIndex++) {
System.out.print("\t" + headingIndex);
}
System.out.print('\n');
for (int inner = lowerValue; inner <= upperValue; inner++) {
System.out.print(inner);
for (int outer = lowerValue; outer <= upperValue; outer++) {
System.out.print("\t"+(inner*outer));
}
System.out.print('\n');
}
}
}发布于 2013-11-27 10:32:37
尝试一下,它不需要双循环来打印内部输出
int lowerValue = 1;
int upperValue = 10;
for (int headingIndex = lowerValue; headingIndex <= upperValue;
headingIndex++) {
System.out.print("\t" + headingIndex);
}
System.out.print('\n');
int inner = 10;
System.out.print(inner);
for (int outer = lowerValue; outer <= upperValue; outer++) {
System.out.print("\t"+(inner*outer));
}
System.out.print('\n');输出
1 2 3 4 5 6 7 8 9 10
10 10 20 30 40 50 60 70 80 90 100发布于 2013-11-27 10:34:29
这能满足你的需要吗?
public static void main(String[] args) {
int lowerValueHorizontal = 1;
int lowerValue = 10;
int upperValue = 10;
for (int headingIndex = lowerValueHorizontal; headingIndex <= upperValue; headingIndex++) {
System.out.print("\t" + headingIndex);
}
System.out.print('\n');
for (int inner = lowerValue; inner <= upperValue; inner++) {
System.out.print(inner);
for (int outer = lowerValueHorizontal; outer <= upperValue; outer++) {
System.out.print("\t"+(inner*outer));
}
System.out.print('\n');
}
}发布于 2013-11-27 10:38:13
您只需执行第二个for循环即可
public class TimesTable {
public static void main(String[] args) {
int lowerValue = 1;
int upperValue = 10;
for (int headingIndex = lowerValue; headingIndex <= upperValue; headingIndex++) {
System.out.print("\t" + headingIndex);
}
System.out.print('\n');
//for (int inner = lowerValue; inner <= upperValue; inner++) {
int inner = 10;
System.out.print(inner);
for (int outer = lowerValue; outer <= upperValue; outer++) {
System.out.print("\t"+(inner*outer));
}
System.out.print('\n');
//}
}
}学校项目?:)
https://stackoverflow.com/questions/20232780
复制相似问题