2013-03-11 62 views
-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); 

    } 



} 
+0

如果我剛剛粘貼它不是證明我正在做我的作業,那麼我不知道該說什麼m8 – 2013-03-11 21:19:15

+1

我現在問的問題與我以前問過的問題不同。它只是代碼的轉發,要求提出一個不同的問題,因爲我的另一篇文章針對相同的代碼進行了編輯,以滿足我之前詢問的問題的需求。由於我在提出一個新問題,因此我建議爲了社區的利益,爲此做一個新的職位。請如果你沒有回答我的問題,我會請你停止在我的文章發送垃圾郵件。 – 2013-03-11 21:29:03

回答

1

很可能,以下是不是你想要的:

difference=(endingV-startV)/size; 

由於startVendingVsize都是整數,這使用整數(截斷)部門。

此外,創建並使用單個Scanner而不是每次需要讀取值時創建一個新的。

+0

仍然我不明白爲什麼這就是我在我的數組中得到0值的原因。我的意思是說,也許會改變結果,但並不能解釋爲什麼我得到0. – 2013-03-11 21:16:50

0

//calling the functions from the other classes

不,你不知道。你只需創建實例。

+0

該評論來自以前沒有改變它對不起:) – 2013-03-11 21:25:23

+0

這並不改變這種情況。當你調用新的Input()時,你不用數組填充數組。 Input構造函數不知道有關數組的任何內容。它創建自己的數組。 – zeroflagL 2013-03-11 21:55:49