2015-02-05 1 views
0

그래, 아직까지는 꽤 익숙하지 않아 코드에 무슨 일이 일어나고 있는지 모르겠습니다. 그것은 옳았어요,하지만 그것을 실행하려고하면, 제게 오류로 나누어주는거야?인구 밀도 계산하기 : 초보자 용 안내서

public class Country 
{ 
    // fields 
    private String name; 
    private int population; 
    private int area; // in square miles 

    // constructors 

    // methods 
    public String getName() 
    { 
     return name; 
    } 

    public int getPopulation() 
    { 
     return population; 
    } 

    public int getArea() 
    { 
     return area; 
    } 

    public void setName(String newName) 
    { 
     name = newName; 
    } 

    public void setPopulation(int newPopulation) 
    { 
     population = newPopulation; 
    } 

    public void setArea(int newArea) 
    { 
     area = newArea; 
    } 

    // population per square mile 
    public int populationDensity() 
    { 
     int density; 
     density = population/area; // integer division truncates 
     return density; 
    } 
} 

내가했던 또 다른 하나 :

public class A03 
{ 
    public static void main(String[] args) 
    { 
     Country country1 = new Country(); 

     String newName = "Macau"; 
     int newPopulation = 453000; 
     int newArea = 6; 
     int density = newPopulation/newArea; 

     country1.setName(newName); 
     country1.setPopulation(newPopulation); 
     country1.setArea(newArea); 

     System.out.printf("Name: " + country1.getName()); 
     System.out.printf("%nPopulation: %,d ", country1.getPopulation()); 
     System.out.printf("%nArea: "+ country1.getArea());  
     System.out.printf("%nPopulation Density: %,d", country1.populationDensity(), "%n"); 
     System.out.println(); 
     System.out.println(); 

     Country country2 = new Country(); 
     String name1 = "Libya"; 
     int population1 = 5900000; 
     int area1 = 679358; 
     int density1 = population1/area1; 

     System.out.printf("Name: %s%n", name1); 
     System.out.printf("Population: %,d%n", population1); 
     System.out.printf("Area: %,d%n", area1); 
     System.out.printf("Population Density: %,d%", country2.populationDensity(), "%n"); 

     System.out.println(); 

     Country country3 = new Country(); 
     String name2 = "USA"; 
     int population2 = 298500000; 
     int area2 = 3539225; 
     int density2 = population2/area2; 

     System.out.printf("Name: %s%n", name2); 
     System.out.printf("Population: %,d%n", population2); 
     System.out.printf("Area: %,d%n", area2); 
     System.out.println(); 

    } 
} 

사람이 무엇이 잘못되었는지 말해 주실 래요 한 페이지 내 교사

코드 1이 내게 준? 아니면 제가하고있는 일이 잘못되었습니다. 나는 기본적으로 다음 국가의 인구 밀도에 맞게 변수를 복사, 붙여 넣기 및 변경했습니다.

+0

귀하의 국가 2에는 지역이 없습니다 ... – njzk2

답변

4

main에서 country2 또는 country3에 세터 메소드를 호출하지 않았습니다. 변수 만 선언했습니다. 인스턴스 변수에 값이 할당되지 않으면 Java는 기본 값을 할당합니다. 기본 값 유형은 기본 숫자 유형의 경우 0입니다. 왜 제로 오류로 인한 오류가 발생했는지 설명합니다.

country2country3에 대한 세터 (및 getters)에게 전화하십시오.

+0

OMG ... 나는 이것을 잊어 버릴 수 없습니다! :) 잡기에 감사드립니다! :) – james13

0

당신은 COUNTRY2 객체의 인구 및 지역 특성을 설정하지 않은 및 전화

 System.out.printf("Population Density: %,d%", country2.populationDensity(), "%n"); 

당신이 있기 때문에 0으로 나눌 때 populationDensity()에서이 지역으로 분할하고 0 그래서이 설정 아니에요.

관련 문제