简体   繁体   English

将计算值存储到数组中

[英]Storing Calculated Values into an Array

In my program, the equation is solved for y given a range of x values. 在我的程序中,给定x值范围,方程求解了y。 How do I then store those values into y and print them out as the array. 然后,如何将这些值存储到y中,并将其作为数组打印出来。 I thought I was supposed to use the index method but I have an error. 我以为我应该使用索引方法,但是出现错误。 Multiple markers at this line - Syntax error on token "]", VariableDeclaratorId expected after this token - y cannot be resolved to a type 此行上有多个标记-标记“]”上的语法错误,此标记后应有VariableDeclaratorId-y无法解析为一种类型

What do I need to modify? 我需要修改什么?

import java.lang.Math;
import java.util.Arrays;
public class Standard {
    public static void main (String[] args) {
        double exponent,  x, pi, e, sqrtpart;
        double[] y;
        pi = 3.14159;
        e = 2.71828;
        x = -2.0;
        int count = 0;

        while (count < 20)
        {
        exponent = - ((x*x)/(2));

        sqrtpart = Math.sqrt(2*pi);

        y[] = (Math.pow(e,exponent))/sqrtpart;
        System.out.println(y[index]);

        x = x + 0.2;
        count++;
        }
       }
    }

Change 更改

double[] y;

into

double[] y = new double[20];

And

    y[] = (Math.pow(e,exponent))/sqrtpart;
    System.out.println(y[index]);

into

    y[count] = (Math.pow(e,exponent))/sqrtpart;
    System.out.println(y[count]);

You can not put values into the y array as presently it does not have any size. 您无法将值放入y array因为它目前没有任何大小。

Try double y [] = new double [20]; 尝试double y [] = new double [20];

Also

System.out.println(y[index]);

should be 应该

System.out.println(y[count]);

you need to initialize y 您需要初始化y

Put this outside the while loop 将其放在while循环之外

y = new double[<size of your array>];

Then you can use the index method 然后可以使用索引方法

y[index] = (Math.pow(e,exponent))/sqrtpart;

The previous answers help you fix the compiler problems. 前面的答案可帮助您解决编译器问题。 But there are other things you should consider: 但是您还应该考虑其他事项:

  1. You are calculating 1/sqrt(2*pi) in every single iteration in the while loop. 您正在while循环的每个迭代中计算1 / sqrt(2 * pi)。 This is not good from a performance point of view since this calculation will never change. 从性能的角度来看,这是不好的,因为此计算永远不会改变。 So it is better to pull this calculation out of the while loop. 因此最好将此计算从while循环中拉出来。

  2. You don't need to define your own pi and e. 您无需定义自己的pi和e。 Just use the static ones in the Math class: Math.PI, Math.E 只需在Math类中使用静态变量:Math.PI,Math.E

  3. You have hard-coded values in your code for the size of the array and the increments. 您在代码中具有对数组大小和增量进行硬编码的值。 That makes your code fragile. 这会使您的代码脆弱。 You could change one number and miss another. 您可以更改一个数字而错过另一个数字。

So consider the following: 因此,请考虑以下几点:

import java.lang.Math;

public class Standard {

    static int size = 20;
    static double width = 4.0;
    static double increments = width/size; 

    public static void main (String[] args) {

        double exponent, x, sqrtpart;

        double[] y = new double[size];

        x = - (width / 2);

        sqrtpart = Math.sqrt(2* Math.PI);

        int count = 0;
        while (count < size)
        {
            exponent = - ((x*x)/(2));

            y[count] = (Math.pow(Math.E,exponent))/sqrtpart;

            System.out.println(y[count]);

            x = x + increments;

            count++;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM