[英]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
.更改类director
到Director
,和类nintodec
到NinToDec
。 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.