사용자가 길이와 너비를 제공하면 사각형의 영역과 둘레를 인쇄하는 프로그램을 작성하고 있습니다. 사용자가 원하는 사각형을 몇 개 입력했는지를 묻는 다음 for 루프를 사용하여 길이와 너비를 묻습니다.이 길이와 너비는 사용자가 입력 한 사각형의 수에 따라 반복됩니다.데이터 이중 인쇄
내가 원하는대로 작동하지만 사용자가 더 많은 직사각형을 계속 만들고 선택하면 프로그램은 새 데이터를 인쇄하는 대신 이전 데이터와 새 데이터의 결과를 인쇄합니다 .
저는 Java 프로그래밍에있어 매우 익숙하며이를 수정하는 방법에 집착하고 있습니다. 누군가 나를 도울 수 있다면 좋을 것입니다. 감사! 당신은 그래서 당신은 새로운 사각형하여 기존에게의 출력을 추가
newRectangleOutput = newRectangleOutput + "Rect-" + ...
에 해당
newRectangleOutput += "Rect-" + ...
전화
import javax.swing.*;
public class RectangleProgram {
public static void main(String[] args) {
String defaultRectangleOutput = "";
String newRectangleOutput = "";
String finalOutput = "";
int option = JOptionPane.YES_OPTION;
while (option == JOptionPane.YES_OPTION) {
String rectangleNumberString = JOptionPane.showInputDialog(null,
"How many rectangles would you like to create? ",
"Number of Rectangles", JOptionPane.QUESTION_MESSAGE);
if (rectangleNumberString == null) return;
while (rectangleNumberString.equals("")) {
rectangleNumberString = JOptionPane.showInputDialog(
"You have entered nothing.\n" +
"Please try again: ");
}
int rectangleNumber = Integer.parseInt(rectangleNumberString);
while (rectangleNumber <= 0) {
rectangleNumberString = JOptionPane.showInputDialog(
"Entry cannot be 0 or negative.\n" +
"Please try again: ");
if (rectangleNumberString == null) return;
rectangleNumber = Integer.parseInt(rectangleNumberString);
}
for (int i = 0; i < rectangleNumber; i++) {
String lengthString = JOptionPane.showInputDialog(null,
"Enter Length for rectangle: ",
"Getting Length", JOptionPane.QUESTION_MESSAGE);
if (lengthString == null) return;
while (lengthString.equals("")) {
lengthString = JOptionPane.showInputDialog(
"You have entered nothing.\n" +
"Please try again: ");
}
double length = Double.parseDouble(lengthString);
while (length < 0) {
lengthString = JOptionPane.showInputDialog(
"Negative numbers are not allowed.\n" +
"Please try again: ");
if (lengthString == null) return;
length = Double.parseDouble(lengthString);
}
String widthString = JOptionPane.showInputDialog(null,
"Enter Width for rectangle: ",
"Getting Length", JOptionPane.QUESTION_MESSAGE);
if (widthString == null) return;
while (widthString.equals("")) {
widthString = JOptionPane.showInputDialog(
"You have entered nothing.\n" +
"Please try again: ");
}
double width = Double.parseDouble(widthString);
while (width < 0) {
widthString = JOptionPane.showInputDialog(
"Negative numbers are not allowed.\n" +
"Please try again: ");
if (widthString == null) return;
width = Double.parseDouble(widthString);
}
SimpleRectangle newRectangle = new SimpleRectangle(width, length);
newRectangleOutput += "Rect-" + i + " (" + newRectangle.width +
", " + newRectangle.length + ")\n" +
"Area = " + newRectangle.getArea() + "\n" +
"Perimeter = " + newRectangle.getPerimeter() + "\n";
}
SimpleRectangle defaultRectangle = new SimpleRectangle();
defaultRectangleOutput = "Default (" + defaultRectangle.width +
", " + defaultRectangle.length + ")\n" +
"Area = " + defaultRectangle.getArea() + "\n" +
"Perimeter = " + defaultRectangle.getPerimeter() + "\n";
JOptionPane.showMessageDialog(null, defaultRectangleOutput + "\n"
+ newRectangleOutput, "Final Results",
JOptionPane.PLAIN_MESSAGE);
option = JOptionPane.showConfirmDialog(
null, "Would you like to create another rectangle?");
}
}
}
class SimpleRectangle {
double length;
double width;
SimpleRectangle() {
length = 1;
width = 1;
}
SimpleRectangle(double newLength, double newWidth) {
length = newLength;
width = newWidth;
}
double getArea() {
return length * width;
}
double getPerimeter() {
return (2 * (length + width));
}
void setLengthWidth(double newLength, double newWidth) {
length = newLength;
width = newWidth;
}
}
가 교체 설정 확인 '+ ='와'= '는 마지막 직사각형의 세부 사항을 표시합니다. 변수 newRectangleOutput은 다시 초기화되어야합니다. –