我有一个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
}
}
然后在您的解决方案中再添加一个循环,以查找此customer_id是否存在于customerID [customer_count]之前的某个位置上,如果是,则继续下一个customer_count值 – jderda 2012-08-08 08:18:42
的循环,但不幸的是客户ID无法排序。客户:1,交易:3 客户:3,交易:3 客户:0,交易:2 客户:2,交易:1 客户:4,交易:1 – Lukaaaaaaaay 2012-08-08 08:20:24
谢谢你'重新帮助,但我不能使用布尔变量只有int和双 – Lukaaaaaaaay 2012-08-08 08:29:07