2016-09-24 103 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似乎沒有做任何事情。

相關問題