在调用方法时,将static方法中的变量读入main方法作为参数

时间:2013-11-20 19:45:19

标签: java static-methods

我在修复我的程序时遇到了麻烦。在我最后两行代码中,当我为surfaceGravity调用静态方法时,编译器抱怨参数radius与语句“不兼容:double无法转换为double [ ]“当我调用静态方法进行显示时,编译器会使用”找不到符号“语句来抱怨参数g。我该如何解决?任何帮助将不胜感激。以下是我的代码:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class GravityV1{

// calculate surface gravity static method
public static double[] surfaceGravity(double G, double[] M, double[] r){

    // surface gravity variable
    double[] g = new double[M.length];

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

        // equation for calculating surface gravity
        g[i] = (G * M[i]) / Math.pow(r[i], 2);
    }

    // return statement
    return g;
}

// displaying static method

// write results to text file static method
public static String[] writeToFile() throws IOException{

    int index = 0;
    String[] data = new String[index];
    File filename = new File("/Users/timothylee/Gravity1.txt");
    Scanner inFile = new Scanner(filename);
    while(inFile.hasNext()){

        data[index] = inFile.next( );

        index++;

    }

    inFile.close();
    return data;
}

public static void main(String args[]) throws IOException{

    // declare array for months
    String[] Planets = new String[8];

    // initialize array
    Planets [0] = "Mercury";
    Planets [1] = "Venus";
    Planets [2] = "Earth";
    Planets [3] = "Mars";
    Planets [4] = "Jupiter";
    Planets [5] = "Saturn";
    Planets [6] = "Uranus";
    Planets [7] = "Neptune";

    // declare array for mass
    double[] mass = new double[8];

    // initialize array
    mass [0] = 3.30E23;
    mass [1] = 4.869E24;
    mass [2] = 5.97E24;
    mass [3] = 6.4219E23;
    mass [4] = 1.900E27;
    mass [5] = 5.68E26;
    mass [6] = 8.683E25;
    mass [7] = 1.0247E26;

    // declare array for diameter
    double[] diameter = new double[8];

    // initialize array
    diameter [0] = 4880;
    diameter [1] = 12103.6;
    diameter [2] = 12756;
    diameter [3] = 6794;
    diameter [4] = 142984;
    diameter [5] = 120536;
    diameter [6] = 51118;
    diameter [7] = 49532;


    double[] surfaceG = new double[8];
    // // convert to radius

    // declare radius variable
    double radius = 0;

    // for each loop for displaying
    for(double calc : diameter){

        // calculation for converting to radius
        radius =  calc / 2;
    }

    // // calculate surface gravity

    // declare and initialize universal gravity constant;
    double G = 6.67384 * Math.pow(10, -11);

    // call surface gravity method
    surfaceGravity(G, mass, radius);

    // display result
    display(Planets, radius, mass, g);
}
}

5 个答案:

答案 0 :(得分:1)

要修复第一个问题,您应该替换代码:

// declare radius variable
double radius = 0;

// for each loop for displaying
for(double calc : diameter){

    // calculation for converting to radius
    radius =  calc / 2;
}

用这个:

// declare radius variable
double radius[] = new double[diameter.length];

// for each loop for displaying
for (int i = 0; i < radius.length; i++){

   // calculation for converting to radius
   radius[i] =  diameter[i] / 2;
}

要解决第二次问题,您应该替换:

// call surface gravity method
surfaceGravity(G, mass, radius);

用这个:

// call surface gravity method
double[] g = surfaceGravity(G, mass, radius);

答案 1 :(得分:0)

正如编译器所说,radius变量的类型为double,但您的函数需要双精度数组

答案 2 :(得分:0)

double[] surfaceGravity(double G, double[] M, double[] r)

您使用错误的参数类型调用此函数:

surfaceGravity(G, mass, radius);

radius是具有double的原始类型变量。要传递的第三个参数必须是数组。所以你必须传递一个数组来代替半径。更改方法的参数类型:

double[] surfaceGravity(double G, double[] M, double r)
  

,编译器用语句抱怨参数g   “找不到标志”。我该如何解决这个问题?

您尚未在类上下文或g函数上下文中声明任何变量main()。因此,使用相关类型并在适当的上下文中声明它。

答案 3 :(得分:0)

在这一行:

public static double[] surfaceGravity(double G, double[] M, double[] r){

你声明r应该是double数组类型,但是在你的main方法中:

double radius = 0;

您将radius声明为double类型。显然这两者是不相容的。

另外,在您调用的主要方法中:

surfaceGravity(G, mass, radius);

返回一个double数组,你永远不会将返回值赋给任何东西。您可以通过以下方式解决此问题:

double[] g = surfaceGravity(G, mass, radius);

答案 4 :(得分:0)

您正在尝试将double转换为double[]变量,这是没有意义的。

double[] surfaceGravity(double G, double[] M, double[] r)

在原型中,'r'是一个纪念日。但是,“radius”中的surfaceGravity(G, mass, radius);double原语。