2013-04-08 58 views
-1

嗨大家即时得到一点卡住这个代码。除了我想要的随机数加起来到用户定义的数量之外,我已经完全掌握了这一点。例如用户输入23500我希望所有的随机数加起来总和。 这是我迄今为止随机数加入总数

package containerweights; 
import java.util.Random; 
import java.util.Scanner; 
import java.io.*; 
public class Containerweightgenerator { 

    /** 
    * @param args 
    * @throws Exception 
    */ 
    public static void main(String[] args) throws Exception { 
     // TODO Auto-generated method stub 

     Scanner input = new Scanner(System.in); 
     System.out.printf ("%n ***Random Number Genreator*** %n%n"); 
     System.out.print("Enter total: "); 
     double total_weight = input.nextDouble(); 
     System.out.print("Enter amount: "); 
     double total_pallets = input.nextDouble(); 

     double average_weight = total_weight/total_pallets; 
     System.out.printf("%-40s -%10.2f%n", "Average Weight is: ", average_weight); 

     double first_weight = average_weight - 50; 
     double second_weight = average_weight + 50; 

     double START = first_weight; 
     double END = second_weight; 
     Random random = new Random(); 
     for (int idx = 1; idx <= total_pallets; ++idx) 
     { 
      showRandomInteger(START, END, random); 
     } 
     } 
    private static void showRandomInteger(double sTART, double eND, Random aRandom) throws Exception{ 
     if (sTART > eND) 
     { 
      throw new Exception(" Start connot exceed End."); 
     } 
     long range = (long)eND - (long)sTART + 1; 

     long fraction = (long)(range * aRandom.nextDouble()); 
     int randomNumber = (int)(fraction + sTART); 
     System.out.println(randomNumber); 
    } 
     private static void log(String aMessage) 
     { 
      log(aMessage); 
     } 
    } 
+3

会发生什么?...即出现了什么问题? – 2013-04-08 12:01:07

+0

所以*定义金额*是你想通过加总随机数达到的'total_weight',直到达到'total_pallets'?如果'for'loop在达到*定义金额*之前达到'total_pallets',或者它们不相关,会发生什么? – GameDroids 2013-04-08 12:09:54

+1

以下哪项是要求:1)托盘重量的总和必须正好为W. 2)必须正好使用N个托盘。 3)每个托盘重量必须在[W/N-50,W/N + 50]范围内? – 2013-04-08 12:14:08

回答

4

只是给了随机数。如果你给的最后一个数字超过总,而不是数量的返回从总

的差异有了新的要求修改我的回答如下:

int pallets=10; 
int targetWeight=100; 
int totalSoFar = 0; 
int[] palletWeight = new int[pallets]; 

//Give random weight to N-1 pallets 
for (int i=0; i<pallets-1; ++i){ 
    palletWeight[i] = random.nextInt(1.33 * targetWeight/pallets); 
    totalSoFar += palletWeight[i]; 
} 

//Check if we exceeded our target 
if (totalSoFar > targetWeight){  
    while(totalSoFar > targetWeight){ 
     int x = random.nextInt(pallets - 1); //pick a pallet at random 
     int a = random.nextInt(palletWeight[x]); 
     palletWeight[x] -= a; //take of a random 'a' grams out of its weight 
     totalSoFar -= a; 
    } 
} 
//Now we are under the target weight, let the last pallet be the difference 
palletWeight[pallets-1] = targetWeight - totalSoFar; 
+0

是的。它不需要最终的“随机数”是系统生成的,它可以被计算;)Gud想着。 – 2013-04-08 12:11:44

+0

Upvoting实用主义。 – CPerkins 2013-04-08 12:54:43

+0

我需要程序打印随机数到total_pallets的数量。这些数字必须加起来才能达到total_weight。 – TheDoctor 2013-04-08 16:04:54

1

如果Lefteris的回答不为你工作(因为可能的要求是所有的值都是“随机的” - 撇开这意味着什么),你唯一的选择就是重复产生随机数并对它们进行总结。

这很丑,很慢,我更喜欢Lefteris的回答。但他的确意味着系列中的最后一个数字不会是随机的,其尺寸分布可能会使其脱颖而出。