2015-06-11 142 views
-2

這是一個簡單的程序。我被分配存儲在一個數組中的對象。但是因爲我是初學者,所以我不知道如何將對象存儲在數組中。有人可以幫我解決這個問題嗎?在Java中存儲對象數組

import java.util.Scanner; 

    public class MainExample { 

    public static void main(String[] args) { 

    double length; 
    double width; 
    double price_per_sqyd; 
    double price_for_padding; 
    double price_for_installation; 
     String input; 
    double final_price; 
     boolean repeat = true; 


    Scanner keyboard = new Scanner(System.in); 

     while (repeat) 
     { 


    System.out.println("\n" +"What is the length of the room?: "); 
    length = keyboard.nextInt(); 

    System.out.println("What is the width of the room?: "); 
    width = keyboard.nextInt(); 

    System.out.println("What is the price of the carpet per square yard?: "); 
    price_per_sqyd = keyboard.nextDouble(); 

     System.out.println("What is the price for the padding?: "); 
     price_for_padding = keyboard.nextDouble(); 

     System.out.println("What is the price of the installation?: "); 
     price_for_installation = keyboard.nextDouble(); 


     keyboard.nextLine(); 

     System.out.println("\n" + "Type 'yes' or 'no' if this is correct: "); 
     input = keyboard.nextLine(); 

     if ("yes".equals(input)) 
     repeat = true; 
     else 
     repeat = false; 

     } 
    } 
} 
+0

你的問題還不清楚。你想要存儲什麼? – Maroun

+0

使用Object [] objArray = new Object [length]' – Codebender

+0

如果您的代碼沒有,您想要存儲在對象數組中的對象是什麼。 –

回答

0

,你需要創建一個類來保存像這樣的屬性。創建一個構造函數來初始化這些值。

public class Room{ 
    double length; 
    double width; 
    double price_per_sqyd; 
    double price_for_padding; 
    double price_for_installation; 
    String input; 
    double final_price; 
    boolean repeat = true; 

} 

然後在您的主要方法/驅動程序類中創建一個具有此類的類型的數組並存儲相關對象。

Room arr=new Room[100]; 
int count=0; 
    while (repeat) 
     { 


    System.out.println("\n" +"What is the length of the room?: "); 
    length = keyboard.nextInt(); 

    System.out.println("What is the width of the room?: "); 
    width = keyboard.nextInt(); 

    System.out.println("What is the price of the carpet per square yard?:  "); 
    price_per_sqyd = keyboard.nextDouble(); 

     System.out.println("What is the price for the padding?: "); 
     price_for_padding = keyboard.nextDouble(); 

    System.out.println("What is the price of the installation?: "); 
    price_for_installation = keyboard.nextDouble(); 


     keyboard.nextLine(); 

     System.out.println("\n" + "Type 'yes' or 'no' if this is correct: "); 
    input = keyboard.nextLine(); 
arr[count]=new Room(length,width,price1,price2,price3,price4);//call to the constructor 
    if ("yes".equals(input)) 
    repeat = true; 
count++; 
    else 
    repeat = false; 

    } 
} 
+0

非常感謝您的幫助。 – Altnay

+0

不要提到:) –

+0

@ MohammedSohailEbrahim-對不起,但現在我又有了一個問題。 既然對象存儲在數組中,我想從數組中打印對象。例如,用戶輸入(是)並輸入多個房間的大小,但當用戶輸入時(否),它停止。程序停止後,我想重新打印用戶在一行中輸入的所有內容。 我希望我清楚地描述這個問題。 – Altnay

0

雖然不是很優雅,這種玩具程序的快速解決辦法是做一個「房」類的各種屬性,如「長度」,「寬度」,「price_per_sqft」等等,那麼你可以設置每個房間對象的特定屬性並將「房間」對象存儲在「房間」陣列中。

0

你想在數組中存儲什麼?不同的final_price值?

無論如何,因爲您不知道循環將運行多少次,您可能需要一個ArrayList。您可以通過添加ArrayList <Double> prices = new ArrayList <Double>();

然後,在循環結尾添加一行,以將正確的變量存儲到ArrayList中。例如:prices.add(final_price);

如果final_price不是您要存儲的內容,那麼只需將其替換爲您想要存儲的變量即可。

而且,不要忘記,如果你使用一個ArrayList,您需要正確的import語句在你的代碼的頂部:import java.util.ArrayList;