2011-02-26 127 views
0

我已經正式走到了我的繩索的盡頭。我找不到我做錯了什麼。我已經完成了這個程序幾乎完全像我前幾天寫的另一個程序,但我編譯時遇到問題。我不知道爲什麼我在輸出線上出現錯誤。請幫助:多個班級:我在這裏做錯了什麼?

這是RUNNING FILE:

package inventory1; 

import java.util.Scanner; 

public class RunApp { 
    public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    DataCollection theProduct = new DataCollection(); 

    String Name = ""; 
    double pNumber = 0.0; 
    double Units = 0.0; 
    double Price = 0.0; 

    while (true) { 
     System.out.print("Enter Product Name: "); 
     Name = input.next(); 
     theProduct.setName(Name); 
     if (Name.equalsIgnoreCase("stop")) { 
     return;}  
     System.out.print("Enter Product Number: "); 
     pNumber = input.nextDouble(); 
     theProduct.setpNumber(pNumber); 

     System.out.print("Enter How Many Units in Stock: "); 
     Units = input.nextDouble(); 
     theProduct.setUnits(Units); 

     System.out.print("Enter Price Per Unit: "); 
     Price = input.nextDouble(); 
     theProduct.setPrice(Price); 

     System.out.print("\n Product Name:  " + theProduct.getName()); 
     System.out.print("\n Product Number:  " + theProduct.getpNumber()); 
     System.out.print("\n Amount of Units in Stock:  " + theProduct.getUnits()); 
     System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n"); 
     System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice()); 

    } 
    } 
} 

這是數據集合文件:

package inventory1; 

public class DataCollection { 
    String productName; 
    double productNumber, unitsInStock, unitPrice, totalPrice; 

    public DataCollection() { 
    productName = ""; 
    productNumber = 0.0; 
    unitsInStock = 0.0; 
    unitPrice = 0.0; 
    } 

    // setter methods 
    public void setName(String name) { 
    productName = name; 
    } 

    public void setpNumber(double pNumber) { 
    productNumber = pNumber; 
    } 

    public void setUnits(double units) { 
    unitsInStock = units; 
    } 

    public void setPrice(double price) { 
    unitPrice = price; 
    } 

    // getter methods 
    public void getName(String name) { 
    productName = name; 
    } 

    public void getpNumber(double pNumber) { 
    productNumber = pNumber; 
    } 

    public void getUnits(double units) { 
    unitsInStock = units; 
    } 

    public void getPrice(double price) { 
    unitPrice = price; 
    } 

    public double calculatePrice() { 
    return (unitsInStock * unitPrice); 
    } 
} 
+1

1)請格式化你的代碼妥善2)你得到什麼錯誤? – Zulan 2011-02-26 05:45:36

+0

與佐蘭一樣......沒有格式良好的代碼和更多信息,很難提供幫助。 – Endophage 2011-02-26 05:47:46

+0

我格式化他的代碼,長自動縮進! – 2011-02-26 05:55:36

回答

2

它爲什麼不編譯的原因是因爲您的主要代碼:

System.out.print("\n Product Name:  " + theProduct.getName()); 
    System.out.print("\n Product Number:  " + theProduct.getpNumber()); 
    System.out.print("\n Amount of Units in Stock:  " + theProduct.getUnits()); 
    System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n"); 
    System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice()); 

需要getName()但你的getName()實現不存在。你沒有正確的簽名。將它改爲適當的吸氣劑,它應該可以工作。其他獲得者也一樣。

相反的:

// getter methods 
    public void getName(String name) { 
    productName = name; 
    } 

    public void getpNumber(double pNumber) { 
    productNumber = pNumber; 
    } 

    public void getUnits(double units) { 
    unitsInStock = units; 
    } 

    public void getPrice(double price) { 
    unitPrice = price; 
    } 

用途:

// getter methods 
    public String getName() { 
    return productName; 
    } 

    public double getpNumber() { 
    return productNumber; 
    } 

    public double getUnits() { 
    return unitsInStock; 
    } 

    public double getPrice() { 
    return unitPrice; 
    } 
+0

完美的工作。我仍然在學習不同的方法,有時候我會迷失方向。感謝您的幫助@Mohamed Mansour – g3n3rallyl0st 2011-02-26 06:07:28

+0

不用擔心,我強烈建議http://download.oracle.com/javase/tutorial/爲初學者:) – 2011-02-26 06:09:06

0

我相信這個問題是您正在閱讀的值的方式。雖然使用Scanner.next()可以安全地讀取Strings,但對於其他數據結構,在實際讀取值之前,可能需要從輸入流中清除新的行字符。

但是這只是在理論上,因爲你幾乎沒有給你的問題。

相關問題