我希望将值变量的结果传递给另一个类。另外我不应该将计算的返回类型从 void 更改为 String
public class One {
private String value = null;
public void compute(){
Scanner s = new Scanner(System.in);
System.out.println("Enter the String");
value = s.next();
}
public String calculate(){
System.out.println("In Calculate.........."+value);
return value;
}
public static void main(String[] args){
One o = new One();
o.compute();
o.calculate();
}
}
public class Two {
public static void main(String[] args){
System.out.println(new One().calculate());
}
}
它返回空值
它是这样打印的
In Calculate..........null
null