2012-08-08 26 views
0

我有一個uni任務,我必須創建一個Java程序,該程序會產生隨機配對的客戶及其事務總數。在java中只打印一次隨機數組變量

它分爲兩個部分,其中第一部分產生的客戶和他們的交易就像這樣:

Customer ID, Transaction Value 
1, 74.36 
1, 44.98 
3, 6.44 
0, 19.13 
3, 59.44 
2, 81.56 
0, 87.21 
4, 40.9 
1, 42.11 
3, 66.05 

第二彙總交易總數爲每一個客戶,像這樣:

Customer: 1 Transactions: 3.0 
Customer: 1 Transactions: 3.0 
Customer: 3 Transactions: 3.0 
Customer: 0 Transactions: 2.0 
Customer: 3 Transactions: 3.0 
Customer: 2 Transactions: 1.0 
Customer: 0 Transactions: 2.0 
Customer: 4 Transactions: 1.0 
Customer: 1 Transactions: 3.0 
Customer: 3 Transactions: 3.0 

我的問題是第二部分應該只產生一次客戶ID,即1,3,0,2,4。我只能使用int和double變量來完成此操作,而無需創建任何其他數組或結構。我的代碼可以在下面找到。

import java.util.*; 

public class Assignment3 { 
    public static long studentNumber=1234567; 

    public static int customerID[]; 
    public static double transactionValue[]; 

    public static void initialiseData(int size) { 
    customerID = new int[size]; 
    transactionValue = new double[size]; 

    Random rand = new Random(studentNumber); 
    for (int i=0; i<size; i++) { 
     customerID[i] = rand.nextInt(size/2); 
     transactionValue[i] = rand.nextInt(10000)/100.0; 
    } 
    } 

    public static void main(String args[]) { 
    int size=10; 

    initialiseData(size); 

    // Your code should only be below this line 
    double transaction = 0; 
    int customer = 0; 
    int customer_Total = 0; 
    int count = 0; 
    int customer_count = 0; 
    double transaction_Total = 0; 

    System.out.println("Customer ID, Transaction Value"); 

    for (size= 0; size < customerID.length; size++) { 
    customer= customerID[size]; 
    transaction= transactionValue[size]; 
    System.out.println(customer + ", " + transaction); 

    } 

    System.out.println(""); 

    for(customer_count = 0; customer_count < customerID.length; customer_count++) { 
     transaction_Total= 0; 
     customer_Total = customerID[customer_count]; 
     count = customerID[customer_count]; 
     //System.out.println(count); 

     for (int customer_count2 = 0; 
      customer_count2 < customerID.length; 
      customer_count2++) {       
     if (customer_Total == customerID[customer_count2]) { 
      transaction_Total++; 

      //System.out.println(customer_count2); 
     } 
     } 

     System.out.println("Customer: "+ customer_Total + " " + 
         "Transactions: " + transaction_Total); 
    } 

    // Your code should not be below this line 

    } 
} 

回答

1

編輯:此代碼:

for(customer_count = 0; customer_count < customerID.length; customer_count++) { 
    transaction_Total= 0; 
    customer_Total = customerID[customer_count]; 
    count = customerID[customer_count]; 
    //System.out.println(count); 

    for (int customer_count2 = 0; 
     customer_count2 < customerID.length; 
     customer_count2++) {       
    if (customer_Total == customerID[customer_count2]) { 
     transaction_Total++; 

     //System.out.println(customer_count2); 
    } 
    } 

    System.out.println("Customer: "+ customer_Total + " " + 
        "Transactions: " + transaction_Total); 
} 

添加以下後 'transaction_Total = 0;':

int wasBefore = 0; //are you allowed to use boolean variables? 
for (int customer_count3 = 0; 
     customer_count3 < customer_count; 
     customer_count3++) {       
    if (customer_Total == customerID[customer_count3]) { 
     wasBefore = 1; 
    } 
} 
if (wasBefore==1) {continue;} 

這樣你仍然得到顧客的隨機順序沒有repreating他們

+0

然後在您的解決方案中再添加一個循環,以查找此customer_id是否存在於customerID [customer_count]之前的某個位置上,如果是,則繼續下一個customer_count值 – jderda 2012-08-08 08:18:42

+0

的循環,但不幸的是客戶ID無法排序。客戶:1,交易:3 客戶:3,交易:3 客戶:0,交易:2 客戶:2,交易:1 客戶:4,交易:1 – Lukaaaaaaaay 2012-08-08 08:20:24

+0

謝謝你'重新幫助,但我不能使用布爾變量只有int和雙 – Lukaaaaaaaay 2012-08-08 08:29:07

2

我建議你看看如何Collections.shuffle的作品。在你的情況下,你可以創建一個包含所有可能值的數組,然後以隨機順序「隨機」。

+0

我不能使用任何東西,但整數和雙打,所以沒有額外的數組 – Lukaaaaaaaay 2012-08-08 07:53:20

+1

你甚至不能在你的代碼中使用這兩個數組,我很困惑。 – 2012-08-08 07:54:28

+0

數組給我,然後我必須使用int和double變量來處理存儲在它們中的信息。除了給予我的數組外,我無法創建任何數組。那有意義嗎? – Lukaaaaaaaay 2012-08-08 08:01:50

0

所以你的家庭作業有點刺激,因爲不使用數組是不常見的。

但是您可以執行以下操作: 您的客戶ID從0變爲size/2,因此可以編寫一個循環來計算每個可能的客戶的交易。 像這樣

for(int customer_id = 0; customer_id <= size /2 ; customer_id++){ 
    int transaction_sum = 0; 
    for (j= 0; j < customerID.length; j++) 
     if(customerID[j] == i) 
      transaction_sum++; 
    if(transaction_sum > 0) 
     System.out.println("Customer: " + customer_id + 
          " Transactions: " + transaction_sum); 
} 

但這並不是一個很好的解決方案,怎麼一回事,因爲這是大量可能的ID慢。

+0

感謝您的評論。 2 客戶:1交易次數:2 2: 客戶:2交易次數:2 2: 客戶:4交易次數:2 – Lukaaaaaaaay 2012-08-08 08:49:56

+0

不幸的是,它沒有答案即時尋找。其可笑煩人 – Lukaaaaaaaay 2012-08-08 08:53:40

+0

顧客:1,交易:3 顧客:3,交易:3 顧客:0,交易:2 顧客:2,交易:1 顧客:4,交易:1 – Lukaaaaaaaay 2012-08-08 09:00:28