2012-05-01 8 views
0

私は、ユーザーのインターネット速度と写真の幅と高さを受け入れる写真プログラムを作成しています。プログラムは、コスト、ストレージサイズ、転送時間を表示します。私はプログラムを2つの部分に分けました。一つは値を設定し、もう一つはすべてのメソッドを持つクラスです。クラスを使用した終了値の追加Java。

私のプログラムで抱えている主な問題は、入力したすべての写真のすべての値を合計し、最後に価格、ストレージ、時間を表示することです。

これまでは、私が設定した値に時刻、価格、および記憶域を追加するaddTotal()メソッドがあります。何が起こっても、入力された最後の写真の値は、すべての写真の合計の代わりに表示され続けます。例えば

: インターネットの速度が150,000セット、およびサイズ4,5と4.5,4.5と写真2枚があるされている場合は、どのような終わりに表示するために仮定されることである。

総コスト= $ 4.83ストレージ

4.14メガバイト

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

あなたは新しいオブジェクトあなたがループに入るたびに作成しているが、あなたは最後のものだけを保持します。ループの前にオブジェクトを作成する必要があります。

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

ループ終了後に結果を照会します。

+0

それはそれでした。ありがとう、私はそれを考えなかっただろう。 – Titan

関連する問題