2013-03-11 5 views
-5

클래스에 함수를 넣으면 결과가 제 배열로 제로 (0)가됩니다. 왜 그런가요? 변경된 사항 없음 수업에 붙여 넣기 만하면됩니다. 그 일을하기 위해 수업에 넣어야 할 것이 있습니까?배열의 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 –

+1

나는 이전에 물어 본 것과는 다른 질문을하고있다. 같은 코드에 대한 다른 포스트가 내가 전에 물어 본 질문의 필요에 맞게 편집 된 이후 다른 질문을하는 코드의 재 게시. 나는 새로운 질문을하고 있기 때문에 커뮤니티의 이익을 위해 새로운 포스트에 그것을하도록 권고 받았다. 제 질문에 대한 답변이 없으면 제 우편에 스팸을 중단 해달라고 부탁드립니다. –

답변

1

다음과 같은 당신이 원하는 것을하지 가능성이 높습니다 : 모든 정수

difference=(endingV-startV)/size; 

endingV, startV 이후 및 size,이 정수 (절단) 사업부를 사용합니다.

또한 값을 읽을 때마다 새 값을 작성하는 대신 단일 Scanner을 작성하여 사용하십시오.

+0

아직도 배열에 0 값이있는 이유가 그 이유를 알 수 없습니다. 내 말은 아마도 결과가 바뀌 겠지만 왜 내가 0을 얻는지를 설명하지 못한다는 것입니다. –

0

//calling the functions from the other classes

아니, 당신은하지 않습니다. 인스턴스 만 만들면됩니다.

+0

그 의견은 이전과 달라서 미안 해요. –

+0

그 상황을 바꾸지 않습니다. new Input()을 호출하면 배열에 값을 채우지 않습니다. Input 생성자는 배열에 대해 아무것도 모릅니다. 자체 배열을 만듭니다. – zeroflagL

관련 문제