This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




7年前关闭。





我必须编写一个程序,每个部分使用不同的类。对于第一堂课:Quest1,我计算了人口增长率。第二堂课:Quest2,我必须将增长存储到一个数组中,在那里我遇到了问题,我将如何去做呢?

public class Quest1 {

public double pop, rate;
public int day;

public void setPop(double population)
{
    pop = population;
}
public double getPOP()
{
    return pop;
}
public void setRate(double rates)
{
    rate = rates;
}
public double getRate()
{
    return rate;
}
public void setDay(int days)
{
    day = days;
}
public double getDays()
{
    return day;
}

public double getNew(double pop, int day, double rate)
{
    double popul, population = 0;
    for (double i = 0; i < day; i++)
    {
        popul = pop + (pop * rate/100);
        population = day*popul;
    }
    return population;
}


}

public class Main {


public static void main(String[] args) throws IOException{
    Scanner kd = new Scanner(System.in);
    Quest1 a = new Quest1();
    Quest2 b = new Quest2();

    double tempPop, tempRate; int tempDay;


    System.out.println("Enter Population: ");
    tempPop = kd.nextDouble();
    a.setPop(tempPop);

    System.out.println("Enter Days: ");
    tempDay = kd.nextInt();
    a.setDay(tempDay);


    System.out.println("Enter Rate: ");
    tempRate = kd.nextDouble();
    a.setRate(tempRate);


    b.storeArray();
}



public class Quest2 {


Quest1 a = new Quest1();

public void storeArray(){
    double scores [] = new double[(int) a.getDays()];
    for(int i = 0; i < a.getDays(); i++)
    {
        scores[i] = a.getNew(a.getPOP(), i+1, a.getRate());
        System.out.println(scores[i]);
    }
    return;
}

最佳答案

我认为您需要将quest1对象传递给quest2类。

b.storeArray(a);


您正在quest2。中创建Quest1的新对象,因此不会计算增长。
在quest2类中进行修改。

public void storeArray(Quest1 a){..}


希望你在问这个。

10-08 19:29