用户输入

时间:2017-10-21 06:30:55

标签: java arrays

基本上我有代码允许用户创建一个数组,但由于某种原因,值保持在0.0

以下是代码:



/**  
 * @author Brian 
 * @version 21 Oct 2017 
 */
/**
 * Example of program execution:
 * Using arrays, and finding the average, max and sum.
 * The array with 4 elements is [4.5, 2.0, 1.2, 3.3]
 * The sum of [4.5, 2.0, 1.2, 3.3] is 11.00 
 * The Average of [4.5, 2.0, 1.2, 3.3] is 2.75 
 * The minimum of [4.5, 2.0, 1.2, 3.3] is 1.20 
 * How many numbers: 3
 * Enter lower and upper range limits: 1 100
 * Enter number 0: 2
 * Enter number 1: 2
 * Enter number 2: 2
 * The sum of [0.0, 0.0, 0.0] is 0.00  
 * The Average of [0.0, 0.0, 0.0] is 0.00  
 * The minimum of [0.0, 0.0, 0.0] is 0.00  
 * After fill with 1.2, the array is [1.2, 1.2, 1.2, 1.2, 1.2, 1.2] 
 */
import java.util.Scanner;

public class EngArray {
  /**
   * Test the array methods
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    System.out.println("Using arrays, and finding the average, max and sum.");
    // a) Declare and initialise an array to hold the real data values 2.3, 4.6, 3.0 and 1.1. 
    double[] array1 = {
      4.5,
      2.0,
      1.2,
      3.3
    };
    // b) Using the provided toString( ) method display the array values 
    // Example code to test method toString( ) below, replace test array with the above array name.
    String array1Str = toString(array1);
    System.out.println("The array with 4 elements is " + array1Str);
    // c) Invoke calcSum and display the numbers and the sum 
    System.out.printf("The sum of %s is %.2f ", array1Str, calcSum(array1));
    // d) Invoke calcAvg and display the numbers and their average 
    System.out.printf("\nThe Average of %s is %.2f ", array1Str, calcAvg(array1));
    // e) Invoke findMin and display the minimum
    System.out.printf("\nThe minimum of %s is %.2f ", array1Str, findMin(array1));
    // g) Invoke makeArray and then display the sum, average and minimum of new array
    double[] madeArray = makeArray();
    String madeArrayStr = toString(madeArray);
    System.out.printf("The sum of %s is %.2f ", madeArrayStr, calcSum(madeArray));
    System.out.printf(" \nThe Average of %s is %.2f ", madeArrayStr, calcAvg(madeArray));
    System.out.printf(" \nThe minimum of %s is %.2f ", madeArrayStr, findMin(madeArray));
    // h) Invoke fill to set a new array of 6 elements to 1.2 and then display modified array
    double[] arrayofsix = {
      3.4,
      5.1,
      9.8,
      2.2,
      4.0,
      7.6
    };
    fill(arrayofsix, 1.2);
    String arrayofsixStr = toString(arrayofsix);
    System.out.printf(" \nAfter fill with %s, the array is %s ", "1.2", arrayofsixStr);
  }
  public static double calcSum(double[] array) {
    double[] array1;
    double sum = 0;
    for (int counter = 0; counter < array.length; counter++)
      sum += array[counter];

    return sum;

  }

  public static double calcAvg(double[] array) {
    double Avg = 0;
    double sum = 0;
    for (int counter = 0; counter < array.length; counter++)
      sum += array[counter];
    Avg = sum / array.length;

    return Avg;
  }

  public static double findMin(double[] array) {
    double Min = array[0];
    for (int i = 1; i < array.length; i++) {
      if (array[i] < Min) {
        Min = array[i];
      }
    }
    return Min;
  }

  /**
   * Returns a string representation of the array i.e. a comma separated list 
   * of elements within [ ].  Similar to java.util.Arrays.toString(double[] a) method.
   * Each array element will be displayed by String.valueOf(double).
   * @param array  the array whose string representation is required
   * @return a string representation of array
   */
  public static String toString(double[] array) {
    String arrayStr = "[";

    for (int i = 0; i < array.length; i++) {
      if (i > 0) // Insert comma separator after first one
        arrayStr += ", ";
      arrayStr += String.valueOf(array[i]);
    }
    arrayStr += "]";
    return arrayStr;
  }

  /**
   * Input a set of elements and store in the created array. The user specifies
   * the size of the array, and enters the elements.
   * @return the created array
   */
  public static double[] makeArray() // Part f)
  {
    final String format = "%.2f";
    Scanner input = new Scanner(System.in);

    System.out.print("\nHow many numbers: ");
    int numNumbers = input.nextInt();

    System.out.print("Enter lower and upper range limits: ");
    double lower = input.nextDouble();
    double upper = input.nextDouble();

    double[] numArray = new double[numNumbers];


    for (int i = 0; i < numNumbers; i++) {
      double number;
      System.out.printf("Enter number %d: ", i);
      number = input.nextDouble();

      while (number < lower || number > upper) {
        System.out.print("Please re-enter in range ");
        System.out.printf("[" + format + " to " + format + "]: ", lower, upper);
        number = input.nextDouble();
      }
    }
    return numArray;


  }
  /**
   *  initialise each element of the array to the given value.
   */
  public static void fill(double[] array, double value) {
    double[] array6 = new double[6];
    for (int i = 0; i < array.length; i++)
      array[i] = value;
  }
}
&#13;
&#13;
&#13;

我有一种感觉,影响这一点的代码的唯一部分就在这里:

public static double[] makeArray() // Part f)
{
  final String format = "%.2f";
  Scanner input = new Scanner(System.in);

  System.out.print("\nHow many numbers: ");
  int numNumbers = input.nextInt();

  System.out.print("Enter lower and upper range limits: ");
  double lower = input.nextDouble();
  double upper = input.nextDouble();

  double[] numArray = new double[numNumbers];


  for (int i = 0; i < numNumbers; i++) {
    double number;
    System.out.printf("Enter number %d: ", i);
    number = input.nextDouble();

    while (number < lower || number > upper) {
      System.out.print("Please re-enter in range ");
      System.out.printf("[" + format + " to " + format + "]: ", lower, upper);
      number = input.nextDouble();
    }
  }
  return numArray;


}

1 个答案:

答案 0 :(得分:-1)

public static double[] makeArray() // Part f)
{
  final String format = "%.2f";
  Scanner input = new Scanner(System.in);

  System.out.print("\nHow many numbers: ");
  int numNumbers = input.nextInt();

  System.out.print("Enter lower and upper range limits: ");
  double lower = input.nextDouble();
  double upper = input.nextDouble();

  double[] numArray = new double[numNumbers];


  for (int i = 0; i < numNumbers; i++) {
    double number;
    System.out.printf("Enter number %d: ", i);
    number = input.nextDouble();

    while (number < lower || number > upper) {
      System.out.print("Please re-enter in range ");
      System.out.printf("[" + format + " to " + format + "]: ", lower, upper);
      number = input.nextDouble();
    }
    // YOU FORGOT TO ASSIGN THE GENERATED NUMBER TO ARRAY
    numArray[i] = number;
  }
  return numArray;


}