2012-04-30 78 views
0

代碼的全部對象和方法的列表可以在這篇文章的底部找到,但真正令我擔憂的是隻是一部分,迭代通過使用循環

for(zed = 0; zed<photoes.size(); zed++) { 
totesPrice += photoes<zed>.getCost(); 
totesSize += photoes<zed>.getMegabytes(); 
totesSpeed = photoes<zed>.getSpeed(); 
} 

理想的totesPrice,totesSize ,而totesSpeed將是存儲在photoes list數組中的對象的get方法的總和。

// Import classes for class 
import java.util.Arrays; 
import java.util.List; 
import javax.swing.*; 
import java.awt.event.*; 
import java.text.DecimalFormat; 
import java.util.ArrayList; 

public class DigitalMain 
{ 
    public static void main(String args[]) 
    { 
    String cont = "Y"; 
    String heightString,width,bitpsString = ""; 
    String widthString = ""; 
    int zed; 
    double bitps,pHS,pWS = 0.0; 
    double totesSpeed = 0.0; 
    double totesPrice = 0.0; 
    double totesSize = 0.0; 
    DecimalFormat wholeDigits = new DecimalFormat("0"); 
    DecimalFormat dec = new DecimalFormat("0.00"); 

    List<DigitalPhoto> photoes = new ArrayList<DigitalPhoto>(); 
    do 
    { 
    DigitalPhoto photo = new DigitalPhoto(); 
    heightString = JOptionPane.showInputDialog("Please enter height"); 
    pHS = Double.parseDouble(heightString); 
    photo.setHeight(pHS); 
    widthString = JOptionPane.showInputDialog("Please enter width"); 
    pWS = Double.parseDouble(widthString); 
    photo.setWidth(pWS); 
    JOptionPane.showMessageDialog(null, "Height: " + photo.getHeight() + "\nWidth: " + photo.getWidth() + "\nResolution: " + photo.getResolution() + " DPI\nCompression Ratio: " + photo.getCompression() + "\nRequired Storage: " + dec.format(photo.getKilo()) + " Kilobytes.\nPrice of Scanned Photo: $" + dec.format(photo.getCost()) + " dollars."); 
    do 
    { 
    bitpsString = JOptionPane.showInputDialog("Please enter your internet connection speed in BITS not BYTES please."); 
    bitps = Double.parseDouble(bitpsString); 
    } while (bitps < 0 && bitps > 99999999); 
    photo.setSpeed(bitps); 

    for(zed = 0; zed<photoes.size(); zed++) { 
    totesPrice += photoes<zed>.getCost(); 
    totesSize += photoes<zed>.getMegabytes(); 
    totesSpeed = photoes<zed>.getSpeed(); 
    } 
    cont = JOptionPane.showInputDialog("\nType \'Y\' to try again or anything but \'Y\' to use values."); 
    photoes.add(photo); 
    } while (cont.equalsIgnoreCase("Y")); 





    double seconds = transferTime(totesSize, totesSpeed); 
    double minutes = seconds/60; 
    double realsec = seconds % 60; 

    JOptionPane.showMessageDialog(null, "You will be paying: " + totesPrice + "\nRequired Storage is: " + totesSize + "Required time for transfer is: " + wholeDigits.format(minutes) + " minutes, and " + wholeDigits.format(realsec) + " seconds."); 

    } 

    public static double transferTime(double totalStorage, double netSpeed) { 
    double bits, seconds; 
    bits = (totalStorage * 8); 
    seconds = (bits/netSpeed); 
    return seconds; 
    }; 
} 
+5

問題是什麼? – Paaske

回答

0
for(zed = 0; zed<photoes.size(); zed++) 
{ 
    totesPrice += photoes.get(zed).getCost(); 
    totesSize += photoes.get(zed).getMegabytes(); 
    totesSpeed = photoes.get(zed).getSpeed(); 
} 

試試這個。

3

我覺得你有你的仿製藥.. for:each
ü可以試試這個...

for (DigitalPhoto digitalPhoto : photoes) { 
     totesPrice += digitalPhoto.getCost(); 
     totesSize += digitalPhoto.getMegabytes(); 
     totesSpeed = digitalPhoto.getSpeed(); 
    } 
+0

plz如果你投下了票,請發表評論...謝謝 –

0

你可以使用,而不是一個Iterator:

Iterator it = photoes.iterator(); 
    while(it.hasNext()) 
    { 
    DigitalPhoto temp = it.next(); 
    totesPrice += temp.getCost(); 
    totesSize += temp.getMegabytes(); 
    totesSpeed = temp.getSpeed(); 
    }