嗨大家即时得到一点卡住这个代码。除了我想要的随机数加起来到用户定义的数量之外,我已经完全掌握了这一点。例如用户输入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);
}
}
会发生什么?...即出现了什么问题? – 2013-04-08 12:01:07
所以*定义金额*是你想通过加总随机数达到的'total_weight',直到达到'total_pallets'?如果'for'loop在达到*定义金额*之前达到'total_pallets',或者它们不相关,会发生什么? – GameDroids 2013-04-08 12:09:54
以下哪项是要求:1)托盘重量的总和必须正好为W. 2)必须正好使用N个托盘。 3)每个托盘重量必须在[W/N-50,W/N + 50]范围内? – 2013-04-08 12:14:08