这是我试图解决的问题。
“墨西哥人口为 6200 万,每年以 7% 的速度增长。美国目前的人口为 2.8 亿,每年以 2% 的速度增长。如果这两个国家保持目前的增长率增长,多少年后墨西哥的人口将超过美国的一半?您的程序应该回答这个问题。”
好的,这是我到目前为止的代码。当我运行该程序时,我收到此错误。
不太确定如何修复它。有人可以帮忙吗? :/
import java.util.Scanner;
public class Whatever {
public static void main (String [] args){
Scanner in = new Scanner (System.in);
int mex = 62000000;
int usa = 280000000;
int years = 0;
double t = 0 ;
while(mex(Math.pow(1.07, t)) <= usa(Math.pow(1.02, t)))
{
t++;
years = t;
if (mex > (usa * 0.5));
break;
}
System.out.println ("Mexicos population is half of America in " + years + "years");
}
}
编辑
对于任何想知道我最终让代码正常工作的人来说。这是代码。
导入java.util.Scanner;
公开课随便{
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
double mex = 62000000;
double usa = 280000000;
double years = 0;
while(mex <= usa/2)
{
years++;
mex = mex * 1.07;
usa = usa * 1.02;
}
System.out.println ("Mexicos population is half of America in " + years + " years ");
}
}
最佳答案
mex 是一个整数。
我认为你试图繁殖。
如果要相乘,请使用 mex * (Math.pow(1.07, t)。
关于java - java新手,不明白为什么我的while循环程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253388/