-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;

startVendingVおよびはすべて整数であるためsize、これは整数 (切り捨て) 除算を使用します。

Scannerまた、値を読み取る必要があるたびに新しいものを作成するのではなく、単一のものを作成して使用してください。

于 2013-03-11T21:09:46.487 に答える
0

//calling the functions from the other classes

いいえ、しません。インスタンスを作成するだけです。

于 2013-03-11T21:22:02.310 に答える