双重打印数据

时间:2014-02-28 23:14:31

标签: java

我正在编写一个程序,用于在用户提供长度和宽度时打印矩形的区域和周长。要求用户输入他/她想要的矩形数,然后我使用for循环来询问长度和宽度,根据用户输入的矩形数量进行循环。

它打印并按照我想要的方式工作......但是,当用户选择继续并创建更多矩形时,程序将打印旧数据的结果和新数据,而不是仅打印新数据。

我对使用Java进行编程非常陌生,并坚持如何解决这个问题。如果有人可以帮助我,那就太好了。谢谢!

这是我的代码:

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;
  }

}

3 个答案:

答案 0 :(得分:1)

你打电话

newRectangleOutput += "Rect-" + ...

相当于

newRectangleOutput = newRectangleOutput + "Rect-" + ...

所以你添加新矩形的输出。将+=替换为=,这就是您想要的。

答案 1 :(得分:0)

您将新的矩形附加到相同的字符串。在所有矩形的for循环之前,在while循环中添加newRectangleOutput = "";

while (option == JOptionPane.YES_OPTION) {
    .
    .

    newRectangleOutput = "";

    for (int i = 0; i < rectangleNumber; i++) {
        .
        .
    }
}

答案 2 :(得分:0)

看看你在这里做了什么......

newRectangleOutput += "Rect-" + i + " (" + newRectangle.width +
            ", " + newRectangle.length + ")\n" + 
            "Area = " + newRectangle.getArea() + "\n" +
            "Perimeter = " + newRectangle.getPerimeter() + "\n";

你再次将newRectangleOutput添加到字符串中......这就是为什么你会看到之前的值......

修复它...在开始下一个矩形之前,即每个循环结束...确保设置newRactangleOutput = "";