在java中只打印一次随机数组变量
我有一个uni的任务,我必须创建一个Java程序,生成随机配对的客户和他们的交易总数 它分为两部分,第一部分产生客户及其交易,如下所示:在java中只打印一次随机数组变量,java,arrays,Java,Arrays,我有一个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 Custom
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
}
}
import java.util.*;
公开课作业3{
公共静态长学号=1234567;
公共静态int customerID[];
公共静态双事务值[];
公共静态void initialiseData(整数大小){
customerID=新整数[大小];
transactionValue=新的双精度[大小];
随机随机数=新随机数(学生数);
对于(int i=0;iEDIT):在该代码中:
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);
}
for(customer\u count=0;customer\u count
在“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;}
int wasBefore=0;//是否允许使用布尔变量?
对于(int customer_count3=0;
客户计数3<客户计数;
客户计数3++{
如果(客户总数==客户ID[客户计数3]){
wasBefore=1;
}
}
如果(wasBefore==1){continue;}
通过这种方式,您仍然可以获得客户的随机订单,而无需重新创建他们编辑:在这段代码中:
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);
}
for(customer\u count=0;customer\u count
在“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;}
int wasBefore=0;//是否允许使用布尔变量?
对于(int customer_count3=0;
客户计数3<客户计数;
客户计数3++{
如果(客户总数==客户ID[客户计数3]){
wasBefore=1;
}
}
如果(wasBefore==1){continue;}
通过这种方式,您仍然可以获得随机顺序的客户,而无需重新创建他们我建议您查看其工作原理。在您的情况下,您可以创建一个包含所有可能值的数组,然后按随机顺序进行“洗牌”。我建议您查看其工作原理。在您的情况下,您可以创建一个包含所有可能值的数组并进行“洗牌”然后是随机顺序。所以你的作业有点烦人,因为不使用数组这样的限制是不常见的
但您可以执行以下操作:
您的客户ID从0变为大小/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);
}
for(int customer\u id=0;customer\u id 0)
System.out.println(“客户:+Customer\u id+
“交易:”+交易金额);
}
但这不是一个好的解决方案,因为对于大量可能的ID来说,这是一个缓慢的过程。所以你的作业有点烦人,因为不使用数组这样的限制是不常见的
但您可以执行以下操作:
您的客户ID从0变为大小/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);
}
for(int customer\u id=0;customer\u id 0)
System.out.println(“客户:+Customer\u id+
“交易:”+交易金额);
}
但这不是一个好的解决方案,因为这对于大量可能的ID来说速度很慢。我只能使用int和double,所以没有额外的数组。你甚至不能在代码中使用这两个数组,我很困惑。数组是给我的,然后我只能使用int和double变量来处理存储在其中的信息。我无法创建吃除给我的数组之外的任何数组。这有意义吗?是的。所以我会把你想要的客户ID放入数组中,然后将它们随机排列。它们是随机的但唯一的(或者出现你想要的任何特定数字)是的,但是它非常简单,你可以在你的代码中做同样的事情。我只能使用int和double,所以没有额外的数组。你甚至不能在你的代码中使用这两个数组,我很困惑。数组是给我的,然后我必须只使用int和double变量来处理存储在其中的信息。我不能创建任何数组,除了给我的那些。这有意义吗?是的。所以我会把你想要的客户ID放入数组中,然后将它们随机排列。它们是随机但唯一的(或者出现你想要的任何特定数字)是的,但它非常简单,您可以在代码中执行相同的操作。然后在解决方案中再添加一个循环,以确定customerID[customer\u count]之前的某个位置上是否存在此特定的客户id,如果存在,请与下一个客户继续循环