-5

将函数放入类中后,我的数组中得到零(0)结果。这是为什么?什么都没有改变我只是在课堂上复制粘贴它们?我还应该在课堂上放什么东西来完成这项工作吗?

import java.util.*;//import library

class Input
{

    public Input (int size,int startV,int endingV)
      {

         //declarations of variables
        double difference;
        double[] array= new double[size];

        array[0]=startV;

     //calculating the difference to add on each number in the array
     difference=(endingV-startV)/size;

    for (int counter=1;counter<size;counter++) //for loop to fill the array
              {
        array[counter]=array[counter-1] + difference;           
          }


      }

    public Input enter(int size,int startV,int endingV)
      {

        //declarations of variables
        double difference;
        double[] array= new double[size];

        array[0]=startV;

             //calculating the difference to add on each number in the array
        difference=(endingV-startV)/size; 

            for (int counter=1;counter<size;counter++) //for loop to fill the array
        {
            array[counter]=array[counter-1] + difference;           
        }

            return this;
    }
}

class Show
{
    public Show (int size,double[] array)
    {

        for (int i=0;i<size;i++) //for loop to print the array
            System.out.println("This is the array " + i+ ": " + array[i]);

    }

    public Show print(int size,double[] array)
    {

        for (int i=0;i<size;i++) //for loop to print the array
        System.out.println("This is the array " + i+ ": " + array[i]);

        return this;
    }
}

public class Assignment2 
{

    public static void  main(String[] args)
    {
        //declaring variables
        int startV,endingV;
        int size=0;



        System.out.print("Give the size of the array:");//Print message on screen
        size = new Scanner(System.in).nextInt();//asking for the size of array

            double[] array= new double[size]; //creation of array

    System.out.print("Give the starting value of the array:");
    startV = new Scanner(System.in).nextInt();//asking for the starting value of array

    System.out.print("Give the ending value of the array:");
    endingV = new Scanner(System.in).nextInt();//asking for the last value of array

    //calling the functions from the other classes 

        Input enter= new Input(size,startV,endingV);
        Show print= new Show(size,array);

    }



}
4

2 回答 2

1

以下内容可能不是您想要的:

difference=(endingV-startV)/size;

由于startV和都是整数,因此使用整数(截断)除法endingVsize

Scanner此外,每次需要读取一个值时,创建并使用一个而不是创建一个新的。

于 2013-03-11T21:09:46.487 回答
0

//calling the functions from the other classes

不,你没有。您只需创建实例。

于 2013-03-11T21:22:02.310 回答