2016-09-24 80 views
0

我已编码的只是跳过for循环干脆以下...我可以创建一个调用某个类的方法的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 
    } 

} 

牢记的是正在使用的不是每个方法,因此它是非常washfull,但我是在第一次尝试左右。

从类中调用数据与WHILE循环一起工作,但不与FOR循环一起使用。

请帮忙!

THanks。

+1

在for循环中,您使用的条件是'pop2.usa pop2.mex || pop2.usa> pop2.jap'。您需要切换最有可能的状况 – Orin

回答

0

在其中运作的,你在循环中有

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

循环不工作,你必须条件

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

我假设你是使用><有差别,所以我会尽量使它们相同。

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

相关问题