java - java 方法和声明

标签 java variables methods declaration

我正在尝试制定一个程序来确定学校的平均分。我将把所有文件保存到计算机上,以便轻松访问它们以供多种用途。我声明创建多种方法并发现了一个问题。我在 startup 方法中拥有主题的用户输入,但在 main 方法中,使用了 sub (主题字符串),其内容如下“sub 无法解析为变量”我明白为什么这么说,但我不确定如何修复。 这是我的代码:

public class Calculator {
static int x;
static int b;


public static void startup() {
    System.out.println("**Receiving User**");
    String user = System.getProperty("user.home");
    System.out.println("**Checking Directories**");
    boolean dir = new File(user + "/Library/Application Support/Average_Calculator/Settings/").mkdirs();
    if (dir) {
        System.out.println("**Directory Created at:" + user + "/Library/Application Support/Average_Calculator/**");
    } else {
        System.out.println("**Directory Has Already Been Created at:" + user
                + "/Library/Application Support/Average_Calculator/**");
    }
    System.out.println("Welcome to the Average Calculator");
    System.out.println("Please input the subject average you want to calculate(no caps)");
    Scanner scan = new Scanner(System.in);
    String sub = scan.nextLine();
    // System.out.println(sub);
    try {
        // System.out.println("It Does!");
        FileOutputStream saveFile = new FileOutputStream(
                user + "/Library/Application Support/Average_Calculator/" + sub + ".sav");
        ObjectOutputStream save = new ObjectOutputStream(saveFile);
        FileOutputStream SetsaveFile = new FileOutputStream(
                user + "/Library/Application Support/Average_Calculator/Settings/" + "Setting" + sub + ".sav");
        ObjectOutputStream setsave = new ObjectOutputStream(SetsaveFile);
        // Create an ObjectOutputStream to put objects into save file.

        // Close the file.
        save.close();
        setsave.close();// This also closes saveFile.
    } catch (Exception exc) {
        exc.printStackTrace(); // If there was an error, print the info.
    }

}

public static void main(String[] args) {
    startup();
    System.out.println(sub);
    try {
        // Open file to read from, named SavedObj.sav.
        FileInputStream saveFile = new FileInputStream(sub + ".sav");

        // Create an ObjectInputStream to get objects from save file.
        ObjectInputStream save = new ObjectInputStream(saveFile);

        x = (Integer) save.readObject();
        b = (Integer) save.readObject();

        // Close the file.
        save.close(); // This also closes saveFile.
    } catch (Exception exc) {
        // exc.printStackTrace(); // If there was an error, print the info.
    }

    // Print the values, to see that they've been recovered.
    System.out.println(x);
    System.out.println(b);

    // All done.
}
}

感谢您的帮助! PS 我对方法和类很陌生,如果能得到解释,我将不胜感激!

最佳答案

sub 当前是 startup() 的局部变量,因此 main() 无权访问它。

一种解决方案是让 startup() 返回 sub 的值,并让 main() 使用该返回值。

第二种解决方案是将 sub(以及任何其他共享变量)声明为 Calculator 类的 static 变量,这将将其放在两个静态方法的范围内。在这种情况下,您不能再在 startup() 中本地声明 sub,因为这会导致该方法忽略同名的静态变量。

关于java - java 方法和声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32280535/

相关文章:

variables - 将 "mut"放在变量名之前和 ":"之后有什么区别?

ruby - 在 Ruby 中创建类方法的不同方式

java - 使用属性匹配为 webDriver 编写 cssselector 表达式

java - 我怎样才能在java中访问NTP时钟

php - 用换行符分解字符串

c++ - 在声明后使用方法扩展类功能

一般的 Javascript 对象无限方法

java - 在基于Soap的Web服务中引发自定义消息异常

java - Spring RestTemplate Jackson 反序列化数组数组

java - Android Studio - 从膨胀 View 自动创建 java 类变量