大家好,我对这段代码有点卡住了。除了我希望随机数加起来达到用户定义的数量外,我已经全部搞定了。例如,用户输入 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);
}
}