[英]Can I create a FOR loop that calls methods from a class?
I have coded the following that just skips the for loop altogether... 我已经编写了以下代码,它们完全跳过了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();
}
}
The I am pulling from another class. 我正在从另一个班级中撤出。 The code for that is bellow:
下面的代码是:
/*
* 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
}
}
Keep in mind not every method is being used so it is pretty washfull but I was experimenting around at first. 请记住,并不是所有方法都在使用,所以它很充实,但是我首先是在尝试。
Calling the data from the class worked with the WHILE loop but not with the FOR loop. 从类中调用数据可以使用WHILE循环,而不能使用FOR循环。
Please help! 请帮忙!
THanks. 谢谢。
In the loop which works you have 在有效的循环中
pop1.usa > pop1.mex || pop1.usa > pop1.jap
in the loop which doesn't work you have the condition 在无效的循环中,您有条件
pop2.usa < pop2.mex || pop2.usa < pop2.jap
I assume whether you use >
or <
makes a difference so I would try to make them the same. 我假设您使用
>
或<
会有所不同,所以我将尝试使它们相同。
BTW usa1
doesn't appear to do anything. 顺便说一句,
usa1
似乎无能为力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.