从Java中的另一种方法使用变量 [英] Using variable from another method in Java

查看:154
本文介绍了从Java中的另一种方法使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算两个粒子,将彼此之间不断反应的位置。我的问题是我需要从把我的颗粒listParticle 阵列我的 READDATA()我的主要(字串[] args)方法。

I want to calculate the position of two particles that will be constantly reacting between each other. My problem is that I need to bring my particle listParticle array from my readData() to my main(String[] args) method.

我曾试图使颗粒阵列的全局变量,但它总是强迫我,使其静态的,然后我的code不起作用。

I have tried making the array of particles a global variable, but it always forces me to make it static and then my code does not work.

我的程序从文件中读取,与数据本身:

My program reads from a file, with the data as such:

2 

1 1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

2 1 1.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

第一个数字是计数,很多粒子是如何在文件中。每个粒子都有一个 ID 键入 X 以Z 的位置,速度和力量。

The first number is count, how many particles are in the file. Each particle has an ID, type, and x, y, and z for position, velocity, and force.

下面是我的code:

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class Particle 
{
    int particleID;
    int particleType;
    double particlePosition[] = new double[3];
    double particleVelocity[] = new double[3];
    double particleForce[] = new double[3];
    static int count;
    static int current = 0;
    static Scanner readData;

    public static void main(String[] args) 
    {
        int k = 100; // in [N/m] or [kg*m/s^2]
        int m = 1; // in [kg]
        double x0 = 1; // in [m]
        double t;  // in [s]
        double dt;  //  in [s]
        double oldForce1;
        double oldForce2;
        double curTime = 0;
        double finTime;

        t = 1/((1/(2*(Math.PI))) * Math.sqrt(2*k/m));
        System.out.println(t);

        dt = t/150;

        readfile();

        //System.out.println("First:  [ " + listParticles[0].particlePosition[0] + " , " + 0 + " ]"); <-- does not work
        //System.out.println("Second:  [ " + listParticles[1].particlePosition[0] + " , " + 0 + " ]"); <-- does not work
    }

    public static void readfile()
    {
        try
        {
            readData = new Scanner(new File("src/2particle-initial.data"));
        }
        catch(Exception e)
        {
            System.out.println("Could not find file");
        }
        count = readData.nextInt();

        Particle [] listParticles = new Particle[count];

        while (current < count)
        {
            listParticles[current] = new Particle();
            listParticles[current].particleID = readData.nextInt();
            listParticles[current].particleType = readData.nextInt();

            listParticles[current].particlePosition[0] = readData.nextDouble();
            listParticles[current].particlePosition[1] = readData.nextDouble();
            listParticles[current].particlePosition[2] = readData.nextDouble();

            listParticles[current].particleVelocity[0] = readData.nextDouble();
            listParticles[current].particleVelocity[1] = readData.nextDouble();
            listParticles[current].particleVelocity[2] = readData.nextDouble();

            listParticles[current].particleForce[0] = readData.nextDouble();
            listParticles[current].particleForce[1] = readData.nextDouble();
            listParticles[current].particleForce[2] = readData.nextDouble();

            current++;
        }
        current = 0;

        System.out.println("First:  [ " + listParticles[0].particlePosition[0] + " , " + 0 + " ]");
        System.out.println("Second:  [ " + listParticles[1].particlePosition[0] + " , " + 0 + " ]");

        readData.close();
    }
}

我怎样才能把我的地方 READDATA() listParticle 阵列到我的

推荐答案

您可以定义方法收益 listParticle 阵列。像这样:

You can define your method to return the listParticle array. Like so:

其签名更改为:

public static Particle[] readfile()

和增加收益后声明关闭

return listParticles;

在你的就可以调用这个函数,并指定其返回给一个变量:

In your main you can call this function and assign its return to a variable:

myParticleArray = Particle.readfile();

这篇关于从Java中的另一种方法使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆