Java 数据的双重打印
我正在编写一个程序,当用户提供长度和宽度时,打印矩形的面积和周长。要求用户输入他/她想要的矩形数量,然后我使用for循环询问长度和宽度,该循环基于用户输入的矩形数量 它打印和工作就像我想要的那样。。。但是,当用户选择继续并创建更多矩形时,程序将打印旧数据加上新数据的结果,而不仅仅是打印新数据 我对Java编程非常陌生,一直在研究如何解决这个问题。如果有人能帮我,那就太好了。谢谢 这是我的代码:Java 数据的双重打印,java,Java,我正在编写一个程序,当用户提供长度和宽度时,打印矩形的面积和周长。要求用户输入他/她想要的矩形数量,然后我使用for循环询问长度和宽度,该循环基于用户输入的矩形数量 它打印和工作就像我想要的那样。。。但是,当用户选择继续并创建更多矩形时,程序将打印旧数据加上新数据的结果,而不仅仅是打印新数据 我对Java编程非常陌生,一直在研究如何解决这个问题。如果有人能帮我,那就太好了。谢谢 这是我的代码: import javax.swing.*; public class RectangleProgra
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;
}
}
import javax.swing.*;
公共类矩形程序{
公共静态void main(字符串[]args){
字符串defaultRectangleOutput=“”;
字符串newRectangleOutput=“”;
字符串finalOutput=“”;
int option=JOptionPane.YES\u选项;
while(option==JOptionPane.YES\u选项){
字符串rectangleNumberString=JOptionPane.showInputDialog(null,
“要创建多少个矩形?”,
“矩形数”,JOptionPane.QUESTION_消息);
if(rectangleNumberString==null)返回;
while(矩形数字字符串等于(“”)){
rectangleNumberString=JOptionPane.showInputDialog(
“您没有输入任何内容。\n”+
“请再试一次:”;
}
int rectangleNumber=Integer.parseInt(rectangleNumberString);
当(矩形号码您呼叫时
newRectangleOutput += "Rect-" + ...
这相当于
newRectangleOutput = newRectangleOutput + "Rect-" + ...
因此,您将新矩形的输出添加到旧矩形中。将+=
替换为=
,这就是您想要的。您将新矩形添加到同一字符串中。在for循环之前的while循环中为所有矩形添加newRectangleOutput=“;
while (option == JOptionPane.YES_OPTION) {
.
.
newRectangleOutput = "";
for (int i = 0; i < rectangleNumber; i++) {
.
.
}
}
while(option==JOptionPane.YES\u选项){
.
.
newRectangleOutput=“”;
对于(int i=0;i<矩形数;i++){
.
.
}
}
看看你在这里做了什么
newRectangleOutput += "Rect-" + i + " (" + newRectangle.width +
", " + newRectangle.length + ")\n" +
"Area = " + newRectangle.getArea() + "\n" +
"Perimeter = " + newRectangle.getPerimeter() + "\n";
您再次将newRectangleOutput添加到字符串中..这就是您看到上一个值的原因
要修复它…在开始下一个矩形之前,即每个循环的末尾…确保设置了newRactangleOutput=“”;
将+=
替换为=
将只显示最后一个矩形的详细信息。需要重新初始化变量newRectangleOutput
。