使用类添加结束值。 Java的。

时间:2012-05-01 18:06:19

标签: java oop class

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

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

到目前为止,我有一个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");
}
}

1 个答案:

答案 0 :(得分:1)

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

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

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