【问题标题】:How to access one variable from one class to another of the same package如何从一个类访问同一个包的另一个变量
【发布时间】:2016-07-29 04:25:00
【问题描述】:

我想从同一个包的另一个 java 类 radioques 中的 java 类计数中访问整数变量 X[0]。我想计算第一个 java 类的 X[0] 和第二个 java 类的 O 的平均值。 这些类是:

package abc;

import java.io.*;
import java.util.*;

public class count {

public static File f = new File("C:/Users/Prashant/Desktop/OCEAN/f.txt");
public static File[] fileList = new File("C:/Users/Prashant/Desktop/OCEAN/Clusters/").listFiles();

public int countWord(String word, File file) throws FileNotFoundException {
    int c = 0;
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
        String nextToken = scanner.next();
        if (nextToken.equalsIgnoreCase(word)) {
            c++;
        }
    }
    scanner.close();
    return c;
}

public static void main(String[] args) throws IOException, FileNotFoundException //, NoSuchElementException
{
    count c = new count();
    c.xyz();
}

public void xyz() throws IOException, FileNotFoundException //, NoSuchElementException
{
    int res = 0;
    int X[] = new int[10];
    for (int i = 0; i < 10; i++) {
        Scanner scanner1 = new Scanner(fileList[i]);
        res = 0;
        while (scanner1.hasNext()) {
            String nextToken1 = scanner1.next();
            count d = new count();
            int s = d.countWord(nextToken1, f);
            res += s;
        }
        System.out.println(res);
        X[i]=res;
        File fl = new File("C:/Users/Prashant/Desktop/OCEAN/res.txt");
        if (!fl.exists()) {
            fl.createNewFile();
        }
        FileWriter fw = new FileWriter(fl, true);
        BufferedWriter bw = new BufferedWriter(fw);
        //bw.write(res+"\n");
        bw.write(fileList[i].getName().toString() + "\t" + res + "\n");
        //bw.write(System.getProperty("line.separator"));
        bw.close();
        scanner1.close();
    }
}
}

另一个类是:

package abc;

import org.apache.jasper.JasperException;

public class radioques {


public void rq(String q[]) throws JasperException, NullPointerException, Exception {
    int ques[] = new int[44];
    int i = 0;
    for (String s : q) {
        ques[i] = Integer.parseInt(s.trim());
        i++;
    }
    int o, c, e, a, n;
    o = ques[4] + ques[9] + ques[14] + ques[19] + ques[24] + ques[29] + (6 - ques[34]) + ques[39] + (6 - ques[40]) + ques[43];
    c = ques[2] + (6 - ques[7]) + ques[12] + (6 - ques[17]) + (6 - ques[22]) + ques[27] + ques[32] + ques[37] + (6 - ques[42]);
    e = ques[0] + (6 - ques[5]) + ques[10] + ques[15] + (6 - ques[20]) + ques[25] + (6 - ques[30]) + ques[35];
    a = (6 - ques[1]) + ques[6] + (6 - ques[11]) + ques[16] + ques[21] + (6 - ques[26]) + ques[31] + (6 - ques[36]) + ques[41];
    n = ques[3] + (6 - ques[8]) + ques[13] + ques[18] + (6 - ques[23]) + ques[28] + (6 - ques[33]) + ques[38];
    float O = (float) o;
    O = O / 50 * 100;
    float C = (float) c;
    C = C / 50 * 100;
    float E = (float) e;
    E = E / 50 * 100;
    float A = (float) a;
    A = A / 50 * 100;
    float N = (float) n;
    N = N / 50 * 100;
    System.out.println(O);
    System.out.println(C);
    System.out.println(E);
    System.out.println(A);
    System.out.println(N);



}

public static void main(String args[]) {

}
}

【问题讨论】:

    标签: java jsp variables scope package


    【解决方案1】:

    您的班级count 没有名为X 的变量。您的方法xyz 有一个名为X 的局部变量,但这仅在一次调用该方法期间有效。

    也许您应该阅读更多有关 Java 基础知识的内容?

    如果xyz 在您想要该值的位置被调用,则从xyz 返回X[0],或者使其成为您可以直接访问或通过getter 方法访问的实例变量。

    【讨论】:

      猜你喜欢
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2020-11-24
      • 2011-10-22
      • 2014-10-28
      • 2015-02-28
      相关资源
      最近更新 更多