如何从 main 方法中获取值以用于另一个 class?
How do you obtain a value from the main method to use in another class?
我目前正在编写一个程序来获取用户对多项式系数和次数的输入,然后将这些输入转换为多项式。在我的主要方法中,我使用扫描仪获取一行字符串,然后将其转换为双精度数组。但是,我需要在另一个 class 中使用这个双数组的值,但似乎无法做到。我已经坚持了一段时间,甚至在研究之后我似乎仍然无法理解它。
PolynomialDemo.java
public class PolynomialDemo {
public static int degree;
public static double[] numbers;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the degree of the first polynomial -> ");
degree = scan.nextInt();
System.out.print("Enter its coefficients in order of descending powers -> ");
String deg = scanner.nextLine();
String[] numbersStr = deg.split(" ");
double[] numbers = new double[ numbersStr.length ];
for ( int i = 0; i < numbersStr.length; i++ )
{
numbers[i] = Integer.parseInt( numbersStr[i] );
}
Polynomial.java
public class Polynomial implements PolyEval {
private double[] coeffs;
private int degree;
public Polynomial() {
//creates polynomial 0
}
public Polynomial(double[] c) throws IllegalArgumentException{
//obtain coefficients
}
@Override
public int degree() {
//return degree
}
@Override
public String toString() {
//build polynomial
}}
Polynomial.java 是我需要获取的值 class 。当我尝试这样做时,值总是 returns 'null' 当它到达时多项式 class。我被困住了,不知道从这里去哪里。感谢您的帮助!
您应该实例化一个 Polinomial
对象并设置您需要的值。
public class PolynomialDemo {
public static int degree;
public static double[] numbers;
public static void main(String[] args) {
//Create a Polynomial object
Polynomial pol = new Polynomial();
Scanner scan = new Scanner(System.in);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the degree of the first polynomial -> ");
degree = scan.nextInt();
System.out.print("Enter its coefficients in order of descending powers -> ");
String deg = scanner.nextLine();
String[] numbersStr = deg.split(" ");
double[] numbers = new double[ numbersStr.length ];
for ( int i = 0; i < numbersStr.length; i++ )
{
numbers[i] = Integer.parseInt( numbersStr[i] );
}
//Set values
pol.setDegree(degree);
pol.setCoefficients(numbers);
}
您需要在 Polynomial
class.
中定义 setDegree(int degree)
和 setCoefficients(double [] coeff)
Setter、构造函数等是 Java 的基础。尝试寻找教程或阅读文档。另外,尝试模块化您的代码。
我目前正在编写一个程序来获取用户对多项式系数和次数的输入,然后将这些输入转换为多项式。在我的主要方法中,我使用扫描仪获取一行字符串,然后将其转换为双精度数组。但是,我需要在另一个 class 中使用这个双数组的值,但似乎无法做到。我已经坚持了一段时间,甚至在研究之后我似乎仍然无法理解它。
PolynomialDemo.java
public class PolynomialDemo {
public static int degree;
public static double[] numbers;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the degree of the first polynomial -> ");
degree = scan.nextInt();
System.out.print("Enter its coefficients in order of descending powers -> ");
String deg = scanner.nextLine();
String[] numbersStr = deg.split(" ");
double[] numbers = new double[ numbersStr.length ];
for ( int i = 0; i < numbersStr.length; i++ )
{
numbers[i] = Integer.parseInt( numbersStr[i] );
}
Polynomial.java
public class Polynomial implements PolyEval {
private double[] coeffs;
private int degree;
public Polynomial() {
//creates polynomial 0
}
public Polynomial(double[] c) throws IllegalArgumentException{
//obtain coefficients
}
@Override
public int degree() {
//return degree
}
@Override
public String toString() {
//build polynomial
}}
Polynomial.java 是我需要获取的值 class 。当我尝试这样做时,值总是 returns 'null' 当它到达时多项式 class。我被困住了,不知道从这里去哪里。感谢您的帮助!
您应该实例化一个 Polinomial
对象并设置您需要的值。
public class PolynomialDemo {
public static int degree;
public static double[] numbers;
public static void main(String[] args) {
//Create a Polynomial object
Polynomial pol = new Polynomial();
Scanner scan = new Scanner(System.in);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the degree of the first polynomial -> ");
degree = scan.nextInt();
System.out.print("Enter its coefficients in order of descending powers -> ");
String deg = scanner.nextLine();
String[] numbersStr = deg.split(" ");
double[] numbers = new double[ numbersStr.length ];
for ( int i = 0; i < numbersStr.length; i++ )
{
numbers[i] = Integer.parseInt( numbersStr[i] );
}
//Set values
pol.setDegree(degree);
pol.setCoefficients(numbers);
}
您需要在 Polynomial
class.
setDegree(int degree)
和 setCoefficients(double [] coeff)
Setter、构造函数等是 Java 的基础。尝试寻找教程或阅读文档。另外,尝试模块化您的代码。