我真的需要一些帮助解决这个问题。我试图用Java代码编写一个函数,但是我被卡住了,我不知道如何继续。我有这个数据包含1行(区)和7列(医院)。该地区有一个百分比,这个人会在医院1结束了,另一个%的将在H2等最终的数据看起来是这样的:如何从阵列中获得第二高值,第三高值等
hospital h1 h2 h3 ...
area
a1 11 45 3 ...
我曾经在一个双阵列保存的数据包含7个值。我有这个随机生成器fcn,它随机选择我们所在的数组中的哪个索引值。如果这个随机生成器选择指数= 0这将意味着11%,如果它随机选择索引= 2,这将意味着3%等
问题是,我做完这个后,我想检查这家医院是否闲置或不。如果它是空闲的,那么我想返回随机选择的整数索引值。如果不是空闲的,我想选择第二高百分比的医院,如果该医院处于空闲状态,则返回此int指数值,否则继续并检查第三高百分比并进行另一次检查等。如果所有医院都没有闲置,则返回随机选择的值。
这是我迄今所做的:
double vector[] = new double[distrToEachHosp.length]();
//start by setting the contents to index
for(int index=0; index<vector.length; index++){
vector[index] = index;
}
for(int i=0; i <= vector.length; i++){
for(int x=1; x <= vector.length; x++){
if (distrToEachHosp[vektor[x]] > distrToEachHosp[vektor[x+1]]){ //compare content that corresponds to index
//move index
int temp = vector[x];
vector[x] = vector[x+1];
vector[x+1] = temp;
}
}
}
return vektor;
int rndValue = randomGenerator(distrToEachHosp);
for(int i=0; i<vector.length; i++){
if(hospital.get(i).namn == hospitalNameVariable[rndValue]){ //hospitalNameVariable is a variable containing the names of the hospitals
if(hospital.get(i).idle()>0){
return rndValue;
} else {
if(vector[0] >= rndValue) {
return vector[0];
} else if(vektor[1] >= slumpVärde) {
......
}
}
}
这是我卡住。我现在用第一种算法对数据进行排序,但从逻辑上讲,我不知道如何包含所有的情况。
你为什么不简单地排序呢? O.o –