我听不懂
我的错误
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable time may not have been initialized
The local variable time may not have been initialized
at earth.main(earth.java:15)
我的编码
public class Earth {
public static void main(String[] args) {
// TODO Auto-generated method stub
int time ;
int distance;
int speed ;
distance = 150000000;
speed = 300000;
distance = speed * time;
System.out.println(+time);
}
}
有趣的是,我可以看到您刚刚开始学习编程,并且对编程和数学之间的关系感到困惑。
您希望对数学等程序进行编程。
在数学中,您定义一个方程:
1500 = 3 * time
然后,您得出答案time = 500
。
编程不能以这种方式工作。编程可以完成计算机的工作,实际上仅是:
fetch data from some memory
perform simple operation (for instance math operation + - * / )
store result back to some memory
在编程中,等号=
与其数学含义完全不同。
在编程中,=
称为assignment
,表示存储计算结果的位置。因此,当您编写时distance = speed * time;
,它不是数学方程式。对于机器,这意味着:
fetch values from memory "speed" and memory "time"
perform math multiply on the two values
store the result back to memory "distance"
显然,这不是您想要的。并且由于您的“时间”不包含任何值,因此Java编译器会抱怨第一步“从“时间”中获取值”。
计算机是愚蠢的,它不能为您解决数学问题,您必须清楚地写下解决方案。
您真正想要的是:
time = distance / speed;
fetch values from distance and speed
perform math divide on the two values
store result back to time
祝您学习顺利:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句