我創建了兩個類,我試圖從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教程。 –