Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java数组赋值-数字文字的使用_Java_Arrays_Double - Fatal编程技术网

java数组赋值-数字文字的使用

java数组赋值-数字文字的使用,java,arrays,double,Java,Arrays,Double,这是一个java类赋值。这一次我的分数很差,因为我的教授说我使用了数字文字。我在40分中输了28分。我设计的解决方案如此糟糕吗?他的确切评论是: Create an application containing an array that stores 20 double values, such as 2.34, 7.89, 1.34, and so on. The application should: * Display the sum of all the arra

这是一个java类赋值。这一次我的分数很差,因为我的教授说我使用了数字文字。我在40分中输了28分。我设计的解决方案如此糟糕吗?他的确切评论是:

Create an application containing an array that stores 20 double values, 
such as 2.34, 7.89, 1.34, and so on. The application should:
     *      Display the sum of all the array elements
     *      Display the average of all the array elements
     *      Display the largest value of all the array elements
我的解决方案是:

 "The program you submitted uses numeric literals in place of the array’s length. 
 This cause several runtime errors when I change the size of your array and tested the code."
import java.util.Random;
导入java.util.array;
公共类MyArray{
公共静态双[]双;
公共静态void main(字符串参数[]){
MyArray.createDoublesArray();
MyArray.displayDoublesArray();
MyArray.displaySum();
MyArray.displayAverage();
MyArray.displayTheLargestValue();
} 
/*填充双数组类成员*/
公共静态void createDoublesArray(){
双打=新双打[20];
//创建随机对象
随机r=新随机();
双量程最小值=1,量程最大值=9;
//生成随机双倍数-在2到9的范围内生成20次

对于(inti=0;i我猜是这样,而不是像下面这样

import java.util.Random; 
import java.util.Arrays;

public class MyArray{ 
  public static double[] doubles;

    public static void main(String args[]) {
       MyArray.createDoublesArray();
       MyArray.displayDoublesArray();
       MyArray.displaySum();
       MyArray.displayAverage();
       MyArray.displayTheLargestValue();
    } 

    /*Fill up the double Array class member*/
    public static void createDoublesArray(){
        doubles = new double[20];
        //Create Random object 
        Random r=new Random(); 
        double rangeMin = 1, rangeMax = 9; 
        //Generate random double number - 20 times within the range of 2 to 9
        for(int i=0;i<20;i++) { 
            //Generate random double numbers and round them to the two decimal places
            double randomdouble = Math.round((rangeMin + (rangeMax - rangeMin) * r.nextDouble())*100.0)/100.0; 
            doubles[i] = randomdouble;            
        }         
    }    

    /*Display the double Array*/
    public static void displayDoublesArray(){
        String delimiter;
        Arrays.sort(doubles);
        System.out.println("The double array: ");
        // iterate through all the array elements
        System.out.print("{");
        for(int i=0;i<20;i++) { 
           if(i < 19){
               delimiter = ", ";
           }
           else{
               delimiter = "}";
           }
           System.out.print(doubles[i]+ delimiter);      
        } 
        System.out.println("\n");
    }

    /*Displays the sum of the double array.*/
    public static void displaySum() {
        //initialize the sum with 0
        double sum = 0.0;

        // iterate through all the array elements
        for (int i = 0; i < doubles.length; i++) {
            // add up each element to the sum variable
            sum += doubles[i];
        }

        // display the sum
        System.out.println("The sum is: " + Math.round(sum*100.0)/100.0 + "\n");
    }

    /*Displays the average of the double array.*/
    public static void displayAverage() {
        // initialize the sum with 0
        double sum = 0.0;

        // iterate through all the array elements
        for (int i = 0; i < doubles.length; i++) {
            sum += doubles[i];
        }
        // display the average by dividing the sum to the length of the array
        System.out.println("The average is: " + Math.round((sum / doubles.length)*100.0)/100.0 + "\n");
    }

    /*Displays the largest value from the double array */
    public static void displayTheLargestValue() {
        //initialize the largest value with the first element
        double largestValue = doubles[0];

        //iterate through the remaining elements (ignore the first)
        for (int i = 1; i < doubles.length; i++) {
            // check if "i" element is greater then the current largest value
            if (doubles[i] > largestValue) {
                largestValue = doubles[i];
            }
        }
        // display the largest value
        System.out.println("The largest value is: " + largestValue);
    }
}  
另外,这里

  for(int i=0;i<doubles.length;i++)

您假设
double
不是空的,当它为空时,将抛出一个异常

import java.util.Random; 
import java.util.Arrays;

public class MyArray{ 
  public static double[] doubles;

    public static void main(String args[]) {
       MyArray.createDoublesArray();
       MyArray.displayDoublesArray();
       MyArray.displaySum();
       MyArray.displayAverage();
       MyArray.displayTheLargestValue();
    } 

    /*Fill up the double Array class member*/
    public static void createDoublesArray(){
        doubles = new double[20];
        //Create Random object 
        Random r=new Random(); 
        double rangeMin = 1, rangeMax = 9; 
        //Generate random double number - 20 times within the range of 2 to 9
        for(int i=0;i<20;i++) { 
            //Generate random double numbers and round them to the two decimal places
            double randomdouble = Math.round((rangeMin + (rangeMax - rangeMin) * r.nextDouble())*100.0)/100.0; 
            doubles[i] = randomdouble;            
        }         
    }    

    /*Display the double Array*/
    public static void displayDoublesArray(){
        String delimiter;
        Arrays.sort(doubles);
        System.out.println("The double array: ");
        // iterate through all the array elements
        System.out.print("{");
        for(int i=0;i<20;i++) { 
           if(i < 19){
               delimiter = ", ";
           }
           else{
               delimiter = "}";
           }
           System.out.print(doubles[i]+ delimiter);      
        } 
        System.out.println("\n");
    }

    /*Displays the sum of the double array.*/
    public static void displaySum() {
        //initialize the sum with 0
        double sum = 0.0;

        // iterate through all the array elements
        for (int i = 0; i < doubles.length; i++) {
            // add up each element to the sum variable
            sum += doubles[i];
        }

        // display the sum
        System.out.println("The sum is: " + Math.round(sum*100.0)/100.0 + "\n");
    }

    /*Displays the average of the double array.*/
    public static void displayAverage() {
        // initialize the sum with 0
        double sum = 0.0;

        // iterate through all the array elements
        for (int i = 0; i < doubles.length; i++) {
            sum += doubles[i];
        }
        // display the average by dividing the sum to the length of the array
        System.out.println("The average is: " + Math.round((sum / doubles.length)*100.0)/100.0 + "\n");
    }

    /*Displays the largest value from the double array */
    public static void displayTheLargestValue() {
        //initialize the largest value with the first element
        double largestValue = doubles[0];

        //iterate through the remaining elements (ignore the first)
        for (int i = 1; i < doubles.length; i++) {
            // check if "i" element is greater then the current largest value
            if (doubles[i] > largestValue) {
                largestValue = doubles[i];
            }
        }
        // display the largest value
        System.out.println("The largest value is: " + largestValue);
    }
}  
另外,这里

  for(int i=0;i<doubles.length;i++)

您假设
doubles
不是空的,当它为空时,将引发异常

以允许我们轻松维护此代码,我首先创建:

    //initialize the largest value with the first element
    double largestValue = doubles[0];
createDoublesArray:

publicstaticvoid createDoublesArray(){
双倍=新的双倍[尺寸];
//创建随机对象
随机r=新随机();
双量程最小值=1,量程最大值=9;
//生成随机双倍数-在2到9的范围内生成20次

double randouble=0.0;//为了使我们能够轻松维护此代码,我首先创建:

    //initialize the largest value with the first element
    double largestValue = doubles[0];
createDoublesArray:

publicstaticvoid createDoublesArray(){
双倍=新的双倍[尺寸];
//创建随机对象
随机r=新随机();
双量程最小值=1,量程最大值=9;
//生成随机双倍数-在2到9的范围内生成20次

我同意马克西姆·舒斯丁的观点

只需添加一条评论

1) 例如,不要求总是使用double

    /*Display the double Array*/
       public static void displayDoublesArray(){
        String delimiter;
        Arrays.sort(doubles);
        System.out.println("The double array: ");
        // iterate through all the array elements

        StringBuilder buff = new StringBuilder(); // use buffer 

        buff.append("{");
        for(int i=0;i<SIZE;i++) { 
           if(i < SIZE-1){
               delimiter = ", ";
           }
           else{
               delimiter = "}";
           }
           buff.append(doubles[i]+ delimiter);                  
        } 

        buff.append("\n");

        System.out.println(buff.toString());
    }

我同意马克西姆·舒斯丁的观点

只需添加一条评论

1) 例如,不要求总是使用double

    /*Display the double Array*/
       public static void displayDoublesArray(){
        String delimiter;
        Arrays.sort(doubles);
        System.out.println("The double array: ");
        // iterate through all the array elements

        StringBuilder buff = new StringBuilder(); // use buffer 

        buff.append("{");
        for(int i=0;i<SIZE;i++) { 
           if(i < SIZE-1){
               delimiter = ", ";
           }
           else{
               delimiter = "}";
           }
           buff.append(doubles[i]+ delimiter);                  
        } 

        buff.append("\n");

        System.out.println(buff.toString());
    }

另外:
if(i<19){delimiter=“,”;}else{delimiter=“}”}如果数组没有正好20个元素,则
将使用错误的分隔符。这不是一个例外,但仍然是不正确的。@JonathannewUI这与op使用数字而不是
数组的所有其他地方基本上是同一个问题。长度
我知道。这不是对您答案的批评,而是对op的一篇帖子指出他/她需要更改的位置。另外:
if(i<19){delimiter=“,”;}else{delimiter=“}”}如果数组没有正好20个元素,则
将使用错误的分隔符。这不是一个例外,但仍然是不正确的。@JonathannewUI这与op使用数字而不是
数组的所有其他地方基本上是同一个问题。长度
我知道。这不是对您答案的批评,而是对op的一篇帖子指出他/她需要改变的地方。