0

我正在编写一个照片程序,它接受用户的互联网速度以及照片的宽度和高度。然后程序会显示成本、存储大小和传输时间。我将程序分成两部分,一个设置值,一个包含所有方法的类。

我的程序遇到的主要问题是将输入的所有照片的所有值相加,并在最后显示价格、存储空间和时间。

到目前为止,我有一个 addTotal() 方法,可以将时间、价格和存储添加到我设置的值中。但是,不断发生的是最后一张输入的照片的值继续显示,而不是所有照片的总数。

例如:如果网速设置为 150,000,并且有两张尺寸为 4,5 和 4.5,4.5 的照片,假设最后显示的是:

总成本 = 4.83 美元

4.14 MB 存储空间

3分40秒转移

任何帮助表示赞赏。

这是主程序:

import javax.swing.JOptionPane;
{
public static void main(String args[])
{
double speed;
double height;
double width;
double compression = 3.5;
double resolution = 600;

System.out.println("Welcome to the Scans-R-Us Digital Photo Shop!");
String userInput = JOptionPane.showInputDialog("Do you have a photo you would like us to scan(Yes or No)?");

while((!userInput.equals("Yes") && (!userInput.equals("YES")) && (!userInput.equals("yes"))))   
    {
    System.out.println("test");
    System.exit(0);
    }
    do  
    {
    String internetString = JOptionPane.showInputDialog("What is your internet speed in bits/second?");
    speed = Double.parseDouble(internetString);
    }
    while(speed < 0);

while((userInput.compareTo("No") != 0) && (userInput.compareTo("NO") != 0) && (userInput.compareTo("no") != 0))
    {
    DigitalPhoto one = new DigitalPhoto();

    do
    {
    String heightString = JOptionPane.showInputDialog("What is the height of the photo?");
    height = Double.parseDouble(heightString);
    }
    while(height >=8);

    do
    {
    String widthString = JOptionPane.showInputDialog("What is the width of the photo?");
    width = Double.parseDouble(widthString);
    }
    while(width >= 8);

    one.setWidth(width);
    one.setHeight(height);
    one.setSpeed(speed);
    one.setCompression(compression);
    one.setResolution(resolution);
    one.setPrice();
    one.setStorage();
    one.setTime();
    one.showValues();
    one.addTotal(); 

    userInput = JOptionPane.showInputDialog("Do you have a photo you would like us to scan(Yes or No)?");

    while((!userInput.equals("Yes") && (!userInput.equals("YES")) && (!userInput.equals("yes"))))
    {
    one.endValues();
    System.out.println("\nThanks, have a nice day!");
    System.exit(0);
    }
 }  
}
}

这是我用来运行程序的类:

import javax.swing.JOptionPane;
{   
double total;
double width;
double height;
double speed;
double compression;
double resolution; 
double price;
double time;
double totalPrice;
double totalStorage;
double totalTime;
double pricePerInch = .12;
int bytes = 8;
int megaSize = 1000000;

DigitalPhoto()
{
width = 0.0;
height = 0.0; 
speed = 0.0;
compression = 0.0;
resolution = 0.0;
total = 0.0;
price = 0.0;
time = 0.0;
}

public void setWidth(double thisWidth)
{
width = thisWidth;
}

public void setHeight(double thisHeight)
{
height = thisHeight;
}

public void setSpeed(double thisSpeed)
{
speed = thisSpeed;
}

public void setCompression(double thisCompression)
{
compression = thisCompression;
}

public void setResolution(double thisResolution)
{
resolution = thisResolution;
}

public double getWidth()
{
return width;
}

public double getHeight()
{
return height;
}

public double getSpeed()
{
return speed;
}

public double getCompression()
{
return compression;
}

public double getResolution()
{
return resolution;
}

public double setStorage()
{
width = width * resolution;
height = height * resolution;
total = width * height;
total = total / compression;
total = total / megaSize;
return total;
}

public double setPrice()
{
price = height * width;
price = price * pricePerInch;
return price;
}

public double setTime()
{
total = total * megaSize;
time = total * bytes;
time = time / speed;
total = total / megaSize;
return time;
}

public void showValues()
{
System.out.println("\nScanned photo details: ");
System.out.println("Resolution: " + resolution);
System.out.println("Compression ratio: " + compression);
System.out.println("The customer photos require " + total + " Mbytes of storage.");
System.out.println("The customer photos cost $" + price);
System.out.println("It will take " + (int)time / 60 + " minutes and " + (int)time % 60 + " seconds");
}

public void addTotal()
{
totalTime = totalTime + time;
totalPrice = totalPrice + price;
totalStorage = totalStorage + total;
}

public void endValues()
{
System.out.println("\nThe  photos require " + totalStorage + " Mbytes of storage.");
System.out.println("The  photos cost $" + totalPrice);
System.out.println("It will take " + (int)totalTime / 60 + " minutes and " + (int)totalTime % 60 + " seconds");
}
}
4

1 回答 1

1

每次进入循环时都会创建一个新对象,因此您只保留最后一个。您需要在循环之前创建对象:

DigitalPhoto one = new DigitalPhoto();
while((userInput.compareTo("No") != 0) && (userInput.compareTo("NO") != 0) &&
   (userInput.compareTo("no") != 0)) {
  //...
  one.setPrice();
  //...
  one.addTotal();
}

然后在循环结束后查询结果。

于 2012-05-01T18:14:43.693 回答