在 Java 中只打印一次随机数组变量



我有一个 uni 的任务,我必须创建一个 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 和双精度变量来做到这一点,而不创建任何其他数组或结构。我的代码可以在下面找到。

 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
  }
}

我建议你看看Collections.shuffle是如何工作的。 在您的情况下,您可以创建一个包含所有可能值的数组,然后以随机顺序"洗牌"。

编辑:在此代码中:

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;}

通过这种方式,您仍然可以获得客户的随机顺序,而不会责备他们

所以你的家庭作业有点烦人,因为像不使用数组这样的限制是不寻常的。

但您可以执行以下操作:您的客户 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 来说,这很慢。

最新更新