到目前为止,这是我的代码,我应该使用从1到10的长度和宽度的所有可能组合打印矩形的区域和周长。我基本上设置了表格,但不要#39 ; t知道如何将面积和周长值放在表格中。
public class Project_6_7 {
public static void main(String[] args) {
int area;
int perimeter;
System.out.println(" Length");
System.out.printf("%10s%5s%5s%5s%5s%5s%5s%5s%5s%5s" , "1" , "2" , "3", "4", "5" , "6" , "7", "8", "9", "10");
for (int width = 1; width <= 10; width++){
System.out.println();
if (width == 5){
System.out.print("Width " + width);
}else
System.out.print(" " + width);
for (int length = 1; length <= 10; length++){
area = width * length;
perimeter = 2 * (length + width);
}
}
}
}
答案 0 :(得分:0)
在你for-loop
内打印同一行的区域和周边,并初始化你的区域和周长。您可以使用2D数组来获得更好的视图。
像这样:
for (int length = 1; length <= 10; length++){
area = width * length;
// perimeter = 2 * (length + width);
System.out.print(area +" ");
}
你必须为周边创建另一个表。
答案 1 :(得分:0)
理想情况下,对于漂亮的打印,在您的情况下,您应该为 area 和 perimeter 设置不同的表。无论如何,我和printf
一起玩,试图最大限度地减少所涉及的代码行,以使它看起来更好,并提出这个,添加评论为什么我使用了什么 -
public static void main(String[] args) {
System.out.printf("%32s", "Length"); // approx half of total chars ((5*10 + 7)/2)
System.out.printf("%7s", ""); // why 7? 5 char 'width' + 1 space + 1 digit
for (int i = 0; i <= 10; i++) {
//row marking
System.out.printf("\n%7s", i == 0 ? "" : i == 5 ? "Width " + i : i); //this i would be width eventually
for (int j = 1; j <= 10; j++) {
int area = i * j; // here j would be the corresponding length
System.out.printf("%5s", i == 0 ? j : area); // this is just the area(1 parameter)
}
}
}
您可以将逻辑移动到一个通用的方法,该方法可以接受参数并为您打印表格。 ENUM行上的东西,你想要打印表格(输入到方法),你可以根据它来操作操作部分。
答案 2 :(得分:0)
我交换了一些代码,因此表格中有更多空间用于您想要的信息,并且可以自由地为每个矩形添加额外的行。由于单位数值与双位数值的比较,我还在进行交错,但这应该暂时有效。
public class PersonalRectanglePrinting {
public static void main(String[] args) {
int[] width = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] length = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[][] areas = new int[width.length][length.length];
int[][] perimeters = new int[width.length][length.length];
for(int currentWidth : width) {
for(int currentLength : length) {
areas[currentWidth - 1][currentLength - 1] =
currentWidth * currentLength;
perimeters[currentWidth - 1][currentLength - 1] =
2 * (currentWidth + currentLength);
}
}
System.out.printf("%39s\n", "Length");
System.out.printf("%10s%6s%6s%6s%6s%6s%6s%6s%6s%6s\n" , "1" , "2" , "3",
"4", "5" , "6" , "7", "8", "9", "10");
for(int currentWidth : width) {
if(currentWidth == 5) {
System.out.printf("Width ");
} else {
System.out.printf(" ");
}
System.out.printf("%s%6s%6s%6s%6s%6s%6s%6s%6s%6s%6s\n", currentWidth,
"A: " + areas[currentWidth - 1][0],
"A: " + areas[currentWidth - 1][1],
"A: " + areas[currentWidth - 1][2],
"A: " + areas[currentWidth - 1][3],
"A: " + areas[currentWidth - 1][4],
"A: " + areas[currentWidth - 1][5],
"A: " + areas[currentWidth - 1][6],
"A: " + areas[currentWidth - 1][7],
"A: " + areas[currentWidth - 1][8],
"A: " + areas[currentWidth - 1][9]);
System.out.printf("%13s%6s%6s%6s%6s%6s%6s%6s%6s%6s\n",
"P: " + perimeters[currentWidth - 1][0],
"P: " + perimeters[currentWidth - 1][1],
"P: " + perimeters[currentWidth - 1][2],
"P: " + perimeters[currentWidth - 1][3],
"P: " + perimeters[currentWidth - 1][4],
"P: " + perimeters[currentWidth - 1][5],
"P: " + perimeters[currentWidth - 1][6],
"P: " + perimeters[currentWidth - 1][7],
"P: " + perimeters[currentWidth - 1][8],
"P: " + perimeters[currentWidth - 1][9]);
}
}
}
还在努力修复那些额外的代码行。希望这有点帮助!