我创建了两个类,我试图从UserInterface类中的用户获取值,但我希望它存储在我的第二个类“Calculator”中。在另一个类中存储值
import java.util.Scanner;
public class UserInterface {
public static void main (String Args[]) {
Calculator calculator = new Calculator();
Scanner input = new Scanner(System.in);
System.out.println("Enter your first value: \t");
input.nextInt(firstValue);
System.out.println("Enter your second value: \t");
input.nextInt(secondValue);
}
}
我想input.nextInt(firstValue);将值传递给下面显示的“计算器”类中的firstValue。
public class Calculator {
public int firstValue;
public int secondValue;
public Calculator(int firstValue, int secondValue) {
this.firstValue = firstValue;
this.secondValue = secondValue;
}
}
在此先感谢。
此代码不会编译。除非你没有向我们展示空的构造函数。 –
'计算器。firstValue'是'public'。什么阻止你直接存储它? –
OP,请在这里发布问题之前阅读基本的Java教程。 –