简体   繁体   English

从另一个类调用时如何保持输出变量

[英]how to keep an output variable when called from another class

In the main class the user inputs a number saved with the variable number but when I call a second class where the variable number is used it says it is unassigned.在主类中,用户输入一个与变量号一起保存的数字,但是当我调用使用变量号的第二个类时,它说它是未分配的。

Here the two classes:这里有两个类:

Main class:主要类:

package maiN;

import java.util.Scanner;

public class director {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter a base nine number");

        String number = keyboard.next();
        nintodec.nintodec();

    }

}

and the called class where the variable number remains unassigned:以及变量号保持未分配的被调用类:

 package maiN;

 public class nintodec {
 public  static void nintodec(){
 number = new StringBuffer(number).reverse().toString();

 int numero=0 ;           
 int mult=1;
    for(int i = 0; i < number.length(); i++)
    {
       int digit = number.charAt(i);
       digit = Character.getNumericValue(digit);

        numero=numero+(mult*digit);

               mult=mult*9;
    }
    System.out.println(numero);

First off, change the method name nintodec() to convertToDec() so it won't be confused with a constructor.首先,将方法名称nintodec()更改为convertToDec()以免与构造函数混淆。

public static void convertToDec() {...}

Now to your question.现在回答你的问题。 You can simply declare number as a parameter in the method.您可以简单地将number声明为方法中的参数。

public static void convertToDec(String number) {...}

When you call the method in the main class, pass in number as the argument.在主类中调用方法时,传入number作为参数。 This gives convertToDec() access to the variable.这使convertToDec()可以访问该变量。

nintodec.convertToDec(number);

As a side note, class names should be in CamelCase .作为旁注,类名应该在CamelCase Change class director to Director , and class nintodec to NinToDec .更改类directorDirector ,和类nintodecNinToDec Let me know if you need additional help.如果您需要其他帮助,请告诉我。

So first of all you should stick to the naming conventions commonly used for Java applications (see Code Conventions for Java Programming Language ) also for the code snippets.因此,首先您应该坚持 Java 应用程序常用的命名约定(请参阅Java 编程语言的代码约定)以及代码片段。 So class names should be in CamelCase, package names all-lowercase, etc...所以类名应该是驼峰式的,包名全小写,等等......

Regarding your question, the current code would not compile as the name number in the method ninetodec can not be resolved to a variable.关于您的问题,当前代码无法编译,因为方法 Ninetodec 中的名称编号无法解析为变量。 This is because the variable "number" defined in the main method of class director is not visible outside of the method.这是因为在类director的main方法中定义的变量“number”在方法之外是不可见的。

When calling打电话时

String number = Scanner.next();

a new local variable with the name "number" of type String is created within the scope of the main method.在 main 方法的范围内创建了一个名为“number”、类型为 String 的新局部变量。 It contains the value returned by the call Scanner.next().它包含调用 Scanner.next() 返回的值。 Only code within the same method block which is following this line, can read and interact with this variable.只有在此行之后的同一方法块中的代码才能读取此变量并与之交互。

It is not visible outside of this method, eg in the nintodec method, so the compiler complains that this variable is unknown.它在这个方法之外是不可见的,例如在 nintodec 方法中,所以编译器抱怨这个变量是未知的。 To make it visible for the nintodec method, I have to explicitly pass it to the method.为了使其对 nintodec 方法可见,我必须将其显式传递给该方法。 So the following change of the code would at least compile所以代码的以下更改至少会编译

import java.util.Scanner;

public class director {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter a base nine number");

        String number = keyboard.next();
        nintodec.nintodec(number);
    }
}

and

public class nintodec {
    public static void nintodec(String number) {
        number = new StringBuffer(number).reverse().toString();

        int numero = 0;
        int mult = 1;
        for (int i = 0; i < number.length(); i++) {
            int digit = number.charAt(i);
            digit = Character.getNumericValue(digit);

            numero = numero + (mult * digit);

            mult = mult * 9;
        }
        System.out.println(numero);
    }
}

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

相关问题 从另一个线程调用时,变量为null - Variable is null when called from another thread 我的方法(正在被另一个类调用)如何从该类获取变量而不将其作为参数传递? - How can my method, which is being called by another class, obtain a variable from that class without passing it as a parameter? 如何继续从另一个类更新arrayList? - How keep updating an arrayList from another class? 无法从 Java Android Studio 中的另一个 class 调用变量 - Cannot called a variable from another class in Java Android Studio 从另一个类访问变量时出错 - Error when accessing variable from another class 如何存根从另一个类调用的类中的方法 - how to stub a method in a class which is called from another class 从另一个类调用时,getActivity()在AsyncTask中返回null - getActivity() returns null in AsyncTask when called from another class 从Activity调用时,另一个AsyncTask类中的ProgressDialog不显示 - ProgressDialog in another AsyncTask class not showing when called from Activity 从另一个类调用时,Java PropertyChangeListener不起作用 - Java PropertyChangeListener not working when called from another class Android-从另一个类调用MediaPlayer不会释放 - Android - MediaPlayer will not release when called from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM