Java 更改循环内部的'double'输出
我想先说我是新来的。我试着做一个for循环,它会告诉我不同的40码短跑时间转换成英里/小时。问题在于显示的输出:Java 更改循环内部的'double'输出,java,for-loop,counter,Java,For Loop,Counter,我想先说我是新来的。我试着做一个for循环,它会告诉我不同的40码短跑时间转换成英里/小时。问题在于显示的输出: 5.96 40 Time is 13.727882855399633 Miles Per Hour 6.96 40 Time is 11.755485893416928 Miles Per Hour 7.96 40 Time is 10.27866605756053 Miles Per Hour 我希望它显示为5.96、5.97、5.98等等,而不是5.96和6.96 有人能
5.96 40 Time is 13.727882855399633 Miles Per Hour
6.96 40 Time is 11.755485893416928 Miles Per Hour
7.96 40 Time is 10.27866605756053 Miles Per Hour
我希望它显示为5.96、5.97、5.98等等,而不是5.96和6.96
有人能理解我在努力做什么,以及解决我遇到的这个问题吗
public class FortyToMPH {
public static void main (String args []) {
double yards, foot, FeetInMiles, SecondsPerHour,FeetLength;
double FortyTime, Minutes, SecondsPerMile, MPH;
int counter;
counter = 0;
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime++) {
yards = 40; // length in yards
foot = yards * 3; // convert to feet
System.out.println();
FeetInMiles = 5280; // The number of feet in a Mile
SecondsPerHour = 3600;
FeetLength = FeetInMiles / foot; // You divide the Feet in Miles by the feet conversion of 40 yards
System.out.println();
SecondsPerMile = FeetLength * FortyTime;
MPH = SecondsPerHour / SecondsPerMile;
System.out.println(FortyTime + " 40 Time is " + MPH + " Miles Per Hour ");
counter++;
// every 10th line, print a blank line
if(counter == 10) {
System.out.println();
counter = 0; // reset the line counter
}
}
}
}
FortyToMPH公共类{
公共静态void main(字符串参数[]){
双码,英尺,英尺英里,第二码,英尺长度;
双四分之一秒,分钟,第二分之一秒,英里/小时;
整数计数器;
计数器=0;
for(FortyTime=4.96;FortyTime问题是在for循环定义中使用了++
运算符:
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime++) {
Marounnaroun正确地指出,使用double作为循环计数器有可能导致严重的浮点算术错误,因此我将for循环改为while循环
++
操作符的意思是“将x的值重新指定为x+1”。它只会给您1的增量(或减量,使用--
)
请注意,这将在完成之前打印出数百行。问题是您在for循环定义中使用了++
运算符:
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime++) {
Marounnaroun正确地指出,使用double作为循环计数器有可能导致严重的浮点算术错误,因此我将for循环改为while循环
++
操作符的意思是“将x的值重新指定为x+1”。它只会给您1的增量(或减量,使用--
)
请注意,这将在完成之前打印出数百行。只需更改for循环,如下所示:
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime=FortyTime+.01)
只需更改for循环,如下所示:
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime=FortyTime+.01)
您需要进行两项更改:
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime=FortyTime+0.01)
以便以正确的精度打印。您需要进行两项更改:
for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime=FortyTime+0.01)
因此它以正确的精度打印。我建议您在循环中使用int
,并在循环中执行double
s的所有计算,以防止浮点运算出现问题:
for(int i = 0; i < something; i++) {
double fortyTime = 4.96 + i;
//...
}
您不希望在程序中出现这样的输出。我建议您在循环中使用int
,并在循环中执行double
s的所有计算,以防止浮点运算出现问题:
for(int i = 0; i < something; i++) {
double fortyTime = 4.96 + i;
//...
}
您不希望在程序中有这样的输出。这里我修改了您提供的代码,将循环中生成的双精度数字打印为两位小数(请参见格式%.2f)(循环的步长在增量变量中定义)
FortyToMPH公共类
{
公共静态void main(字符串参数[])
{
双码,英尺,英尺长;
双四分之一秒=4.96分钟,第二分之一秒,英里/小时;
int计数器=0;
/**
*常数。
*/
最终双英尺英寸英里=5280;
每小时最后双倍秒=3600;
最终双增量=0.01;
最终双端=7.99;
while(fortyTime这里我修改了您提供的代码,以打印循环while(循环的步骤在DELTA变量中定义)中生成的双精度小数(请参见格式%.2f)
FortyToMPH公共类
{
公共静态void main(字符串参数[])
{
双码,英尺,英尺长;
双四分之一秒=4.96分钟,第二分之一秒,英里/小时;
int计数器=0;
/**
*常数。
*/
最终双英尺英寸英里=5280;
每小时最后双倍秒=3600;
最终双增量=0.01;
最终双端=7.99;
虽然(fortyTime为什么不简单地将0.01
添加到fortyTime
而不是++
?另外,请注意并更改变量的名称。也许这一个会有帮助。这个问题的标题不是很好地描述您的问题……为什么不简单地将0.01
添加到fortyTime
关于++
?另外,请注意并更改变量的名称。也许这一个会有帮助这个问题的标题不是很能描述你的问题…@Marounnaroun-Hm,我在这里挠头-告诉我吧?有没有在任何数字上加上百分之一,而精度只有两位数字就可以恢复是否存在舍入错误?是的,请尝试打印此循环的值:for(双i=0;i<1.0;i+=0.1)
。或此:for(双i=0;i<1.0;i+=0.01)
@marounnaroun我现在已经更改了它-这不应该遇到同样的问题。@marounnaroun-Hm,我在这里挠头-启发我吗?有没有在任何数字上加上百分之一,但精度只有两位数会导致舍入错误?是的,尝试打印此循环的值:用于(双i=0;i<1.0;i+=0.1)
。或此:用于(双i=0;i<1.0;i+=0.01)
@marounnaroun我现在已经更改了它-这不应该遇到同样的问题。多谢老兄。这太棒了!我还没有了解DELTA和while。double和final double之间有什么区别?这个DELTA是一个常量。不使用final写入是完全合法的。final的意思是,变量的值不能更改ge一旦分配。常量应具有大写名称(请参见:)哥们,非常感谢。这太棒了!我还没有学习DELTA和while。double和final double之间有什么区别?这个DELTA是一个常量。不使用final写入是完全合法的。final的意思是,变量的值一经赋值就不能更改。常量应该有大写名称(请参阅:)
0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
public class FortyToMPH
{
public static void main (String args [])
{
double yards, foot, feetLength;
double fortyTime = 4.96, minutes, secondsPerMile, mph;
int counter = 0;
/**
* Constants.
*/
final double FEET_IN_MILES = 5280;
final double SECONDS_PER_HOUR = 3600;
final double DELTA = 0.01;
final double END = 7.99;
while (fortyTime <= END)
{
yards = 40; // length in yards
foot = yards * 3; // convert to feet
feetLength = FEET_IN_MILES / foot; // You divide the Feet in Miles by the feet conversion of 40 yards
secondsPerMile = feetLength * fortyTime;
mph = SECONDS_PER_HOUR / secondsPerMile;
System.out.format("%.2f 40 Time is %.2f Miles Per Hour%n", fortyTime, mph);
counter++;
// every 10th line, print a blank line
if(counter == 10) {
System.out.println();
counter = 0; // reset the line counter
}
fortyTime += DELTA;
}
}
}