在创建新对象之后,我想创建一个函数,要求将所需的参数输入到对象中

时间:2015-03-05 11:35:06

标签: java

我正在学习java并且刚刚开始使用对象。 我创建了一个名为Polynom的类,并声明了一些我可以在给定的polinom上执行的函数。

现在我想构建一个函数(或构造函数),只要我声明新的Polynom(Polynom Polynom = new Polynom();),我想要控制台请求polinom的输入。

这是我用来"输入" polinom。

System.out.println("Please enter the higest rank of your polynom");
    int rank = Scanner.nextInt();
    Polynom.setRank( rank );
    for(int i = 0 ; i < rank+1  ; i++ ){
        System.out.println("Please enter the coefficient of x^" + i);
        double coefficient = Scanner.nextDouble();
        Polynom.setCoef(coefficient, i);
    }

我在宣布一个新的政策时,我很难弄清楚如何应对这一点。

我想也许在承包商中调用一个能够做到这一点的功能,但我无法弄明白。

我刚刚开始使用对象和类,所以请不要复杂的工作人员。

到目前为止,这是我工作的代码

import java.util.Scanner;
public class Polynom {
    static Scanner Scanner = new Scanner(System.in);
double[] coefficients;

public  void setRank(int rank){
    coefficients = new double[rank+1];
}

public double compute(double vlaue){
    double result = 0;
    for(int i = 0 ; i < coefficients.length ; i++){
        result = result + (coefficients[i] * Math.pow(vlaue, i));
    }
    return result;
}

public void setCoef(double coefficient,int power){
    coefficients[power] = coefficient;
}

public static void main(String[] args){

    Polynom Polynom = new Polynom();

    System.out.println("Welcome to the polynom vlaue calculaor");
    System.out.println();
    System.out.println("Please enter the higest rank of your polynom");
    int rank = Scanner.nextInt();
    Polynom.setRank( rank );
    for(int i = 0 ; i < rank+1  ; i++ ){
        System.out.println("Please enter the coefficient of x^" + i);
        double coefficient = Scanner.nextDouble();
        Polynom.setCoef(coefficient, i);
    }
    System.out.println("Please enter the number to calculate from");
    double from  = Scanner.nextDouble();
    System.out.println("Please enter the number to calculate to");
    double to  = Scanner.nextDouble();
    System.out.println("Please enter the number of each jump");
    double jump  = Scanner.nextDouble();

    for(double i = from ; i < to ; i = i+jump){
        System.out.println("for x = "+i+" The value is = "+Polynom.compute(i));
    }
}

}

2 个答案:

答案 0 :(得分:1)

这可能有效:

import java.util.Scanner;
class Polynomial
{       
    double[] cofficients;
    public Polynomial(Scanner sc)
    {           
        readpoly(sc);
    }
    public void readpoly(Scanner sc)
    {
       if(sc==null)
          return;
       System.out.println("Please enter the higest rank of your polynom");
       int rank = sc.nextInt();
       cofficients=new double[rank+1];
       for(int i = 0 ; i < rank+1  ; i++ ) 
       {
           System.out.println("Please enter the coefficient of x^" + i);
           cofficients[i]=sc.nextDouble();
       }
    }
}

然后你可以简单地创建这样的对象:

 Scanner scanner=new Scanner(System.in);
 Polynomial p=new Polynomial(scanner);

,输入来自控制台。

通过这种方式,您可以多次使用同一个扫描仪(读取多个多项式),而无需在Polynomial类中存储完整的无关对象。

我建议你不要按类名(或之前使用的任何其他名称)命名变量。

答案 1 :(得分:0)

听起来你只需要在构造函数中传递信息?像这样:

class Polynom {

  private int rank;
  private double[] coeffs;

  public Polynom(double[] coeffs) {
     this.rank = coeffs.length - 1;
     this.coeffs = coeffs;
  }
}

Polynom mypoly = new Polynom(new double[]{1.0, 2.0, 3.0});

但是,如果你这样做,你需要在用户输入时临时存储输入,然后在你拥有所有系数后,在一个操作中创建多边形。