2014-02-28 131 views
1

我正在編寫一個程序,用於在用戶提供長度和寬度時打印矩形的面積和周長。要求用戶輸入他/她想要的矩形數量,然後使用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; 
    } 

} 

回答

0

您正在將新矩形附加到同一個字符串。您之前在while循環添加newRectangleOutput = ""; for循環全部矩形

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

    newRectangleOutput = ""; 

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

你叫

newRectangleOutput += "Rect-" + ... 

這相當於

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

所以你添加新的矩形的舊的輸出那些。用=代替+=,這就是你想要的。

+0

更換'= ='將只顯示最後一個矩形的細節。變量'newRectangleOutput'需要重新初始化。 –

0

先看看您在這裏做過的事......

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

您添加的newRectangleOutput字符串again..that就是爲什麼ü重新看到前值...

解決它......你開始即每一個循環的結束下一個矩形前...確保您設置newRactangleOutput = "";