我可以创建一个从类中调用方法的FOR循环吗?

时间:2016-09-24 23:03:08

标签: java loops methods

我编写了以下内容,只是完全跳过了for循环......

package chapter4;

/*
 * @author Tim Lyons
 */

public class Population_for {

    public static void main(String[] args) 
    {
        Population_while pop2 = new Population_while();

        double usa1;

        for (usa1 = 0; pop2.usa < pop2.mex || pop2.usa < pop2.jap
               ; pop2.calcUsa())
        {
            pop2.calcMex();
            pop2.calcJap();
            pop2.calcIter();
            pop2.calcYear();

            pop2.display();
        }

        pop2.displayIter();
    }
}

我从另一个班级拉。该代码如下:

/*
 * Assume that the population of Mexico is 121 million and that the population
increases 1.05% annually (new population = current population x 1.0105). 
Assume that the population of the United States is 315 million and that the 
population is reduced 0.16% annually (new population = current population 
x 0.9984) . Assume that the population of Japan is 127 million and that the 
population increases 1.01% annually (new population = current population x
1.0101). Write an application that displays the populations for the three 
countries every year until both Mexican and Japanese populations pass US 
population. Display the number of years it took for Mexico’s and Japan’s 
populations to exceed that of the United States. Use both while loop and 
for loop to accomplish the task. Save the two files (one for each type of 
loop) as Population_while.java and Population_for.java
 */

package chapter4;

/*
 * @author Tim Lyons
 */

public class Population_while {
    //Below are the populations for each country
    double mex = 121000000; //Mexico is 121 million
    double usa = 315000000; //United States is 315 million
    double jap = 127000000; //Japan is 127 million

    //Below are the annual rates of increase or decrease
    double mexRate = 1.0105; //Mexico's pop increases 1.05% annually
    double usaRate = 0.9984; //The USA pop is reduced 0.16% annually 
    double japRate = 1.0101; //Japan's population increases 1.01% annually

    //Below I provide a starting year just to make it look nice
    int year = 2016;

    //Below is an int for the number of iterations.
    //This will serve as the number of years it took.
    int iter = 0;

    //Below are the operations that increase or decrease each number.
    //Originally I tried using the set() method. It didn't work.
    //So I created methods within the class to class latter.
    public void calcUsa() 
    {
        this.usa = usa * usaRate;
    }

    public void calcJap() 
    {
        this.jap = jap * japRate;
    }

    public void calcMex() 
    {
        this.mex = mex * mexRate;
    }

    public void calcYear() 
    {
        year++;
    }

    public void calcIter() 
    {
        iter++;
    }


    //Below are the simple get() methods used in the display method.
    public double getusa()
    {
        return usa;
    }

    public double getjap()
    {
        return jap;
    }

    public double getmex()
    {
        return mex;
    }

    public int getyear()
    {
        return year;
    }

    public int getIter()
    {
        return iter;
    }


    //The display() method tht is to be repeated within the loop.
    public void display()
    {
        System.out.println("*********NEW YEAR**********************");
        System.out.println("The population of the United States during " +
             getyear() + " is:");
        //Below is a special print method that eleminates scientific notation.
        System.out.printf("%f\n", getusa());
        System.out.println("The population of Mexico during " +
             getyear() + " is:");
        //Below is a special print method that eleminates scientific notation.
        System.out.printf("%f\n", getmex());
        System.out.println("The population of Japan during " +
             getyear() + " is:");
        //Below is a special print method that eleminates scientific notation.
        System.out.printf("%f\n", getjap());
    }

    public void displayUsa()
    {
        System.out.println("The population of the United States during " +
             getyear() + " is:");
        //Below is a special print method that eleminates scientific notation.
        System.out.printf("%f\n", getusa());
    }    

    public void displayMex()
    {
        System.out.println("The population of Mexico during " +
             getyear() + " is:");
        //Below is a special print method that eleminates scientific notation.
        System.out.printf("%f\n", getmex());
    }

    public void displayJap()
    {
        System.out.println("The population of Japan during " +
             getyear() + " is:");
        //Below is a special print method that eleminates scientific notation.
        System.out.printf("%f\n", getjap());
    }

    //The final display() for the total number of years.
    public void displayIter()
    {
        System.out.println("**********End of Loop**********");
        System.out.println("It took " + getIter() + " years for Mexico's "
            + "and Japan's population to overtake the USA's.");
    }

    public static void main(String[] args) 
    {
        //Here I call the default constructor.
        Population_while pop1 = new Population_while();

        /*
        This loop states reads "WHILE the population of the USA is
        greater than Mexico's OR greater than Japan's execute the following..."
        */

        while (pop1.usa > pop1.mex || pop1.usa > pop1.jap)
        {
            pop1.calcYear();  //increments year
            pop1.calcUsa();  //decrements usa
            pop1.calcJap();  //increments jap
            pop1.calcMex();  //increments mex
            pop1.display();  //calls display() method from class
            pop1.calcIter();  //increments inter
        }

        pop1.displayIter();  //Calls displayiter() method from class
    }

}

请记住并非所有方法都在使用,所以它非常漂亮,但我一开始就在试验。

从类中调用数据使用WHILE循环但不使用FOR循环。

请帮忙!

感谢。

1 个答案:

答案 0 :(得分:0)

在有效的循环中

pop1.usa > pop1.mex || pop1.usa > pop1.jap

在没有工作的循环中你有条件

pop2.usa < pop2.mex || pop2.usa < pop2.jap

我假设您使用><是否有所作为,所以我会尝试将它们设为相同。

BTW usa1似乎没有做任何事情。