Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS for循环未显示正确的结果_Javascript_Html - Fatal编程技术网

Javascript JS for循环未显示正确的结果

Javascript JS for循环未显示正确的结果,javascript,html,Javascript,Html,问题是: 正好10:25:21,车速是6 11分钟后,也就是10:36:21,车速增加了1,7也增加了 从10:46:21到12:00:00,每分钟,车速稳定增加1,8分钟后也是如此 变量日期=新日期(); 日期.设定时间(10);; 日期:25分钟; 日期。设置秒(21); 无功转速; 对于(设i=0;i

问题是:

正好10:25:21,车速是6

11分钟后,也就是10:36:21,车速增加了1,7也增加了

从10:46:21到12:00:00,每分钟,车速稳定增加1,8分钟后也是如此


变量日期=新日期();
日期.设定时间(10);;
日期:25分钟;
日期。设置秒(21);
无功转速;
对于(设i=0;i<95;i++){
如果(i<35){
速度=6;
} 
//10:36:21后增加速度1
否则如果(i<45)
{速度=7;
} 
//10:46:21后速度持续增加1
其他的
{speed++;
} 
日期。设定分钟数(25+1);
文件。写入(日期+“:“+速度+”m/s

”; }
带有

在第35次循环(包括)之前将为真

您可以减去起始值:

if (i < 10) { 
    speed = 6; 
} 
//increase speed by 1 after 10:36:21
else if (i < 20) 
{ speed = 7; 
} 
if(i<10){
速度=6;
} 
//10:36:21后增加速度1
否则如果(i<20)
{速度=7;
} 

我认为您对使用的金额有问题,此外,您还有另一个想法:

var date = new Date();
date.setHours(10);
date.setMinutes(25);
date.setSeconds(21);

var speed; 
var dateToShow;

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}

for (let i =  1; i < 96; i++) { 
    if (i < 11) { 
        speed = 6; 
    } 
    //increase speed by 1 after 10:36:21
    else if (i < 22) 
    { speed = 7; 
    } 
    // speed continously increase by 1 after 10:46:21
    else
    { speed++; 
    } 
    
    dateToShow = addMinutes(date, i); 

    document.write(dateToShow + ":" + speed + "m/s <br></br>")

}
var-date=新日期();
日期.设定时间(10);;
日期:25分钟;
日期。设置秒(21);
无功转速;
var dateToShow;
功能添加分钟数(日期,分钟){
返回新日期(Date.getTime()+分钟*60000);
}
对于(设i=1;i<96;i++){
如果(i<11){
速度=6;
} 
//10:36:21后增加速度1
否则如果(i<22)
{速度=7;
} 
//10:46:21后速度持续增加1
其他的
{speed++;
} 
dateToShow=addMinutes(日期,i);
文件。写入(dateToShow+“:“+速度+”m/s

”) }
我认为这解决了你的问题


我添加了一个额外的函数,将分钟添加到新日期,这样您就不会有更多的问题了

至少可以说,代码中的逻辑很奇怪

与其使用计数器手动计算每分钟,为什么不简单地创建开始和结束行程的日期,并标记速度增加的两个位置:

var startTime=new Date();
开始时间:设定时间(10);
开始时间设置分钟数(25);
启动时间设置秒(21);
var endTime=新日期();
结束时间.设定时间(12);
endTime.setMinutes(0);
endTime.setSeconds(0);
var time1=新日期();
时间1.设定时间(10);
时间1.设定分钟数(36);
时间1.设置秒(21);
var time2=新日期();
时间2.设定时间(10);
时间2.设定分钟数(46);
时间2.设置秒(21);
var ThistTime=开始时间;
var速度=0;
while(这次”;
thisTime.setMinutes(thisTime.getMinutes()+1);

}
在开始编写任何东西之前,您需要先建立一些东西:对象、事实、变量、状态和输出

对象

1哪些对象提供了您需要在代码中使用的内容

2您需要在代码中创建哪些对象

3您需要在代码中操作哪些对象

4哪些对象与代码完全无关

5您将如何处理代码中使用的任何对象

事实

已知的静态事实是什么

哪些值在代码执行期间不会更改,但在代码中是必需的

变量

未知因素是什么

在代码执行期间,哪些值会或可能会改变

声明

1任何对象或变量的起始状态是什么

2任何对象或变量的期望/要求最终状态是什么

输出

1您是否需要提供任何“打印”输出-例如,到屏幕或控制台

2您需要返回任何东西吗?例如,这是一个返回一个或多个值的函数吗


因此,考虑到这一点,如果我们看看你的问题:

对象

有两种:

汽车-完全不相关,因为问题中没有任何内容是基于车辆类型的,因此我们可以忽略此对象

时钟-确定时间流逝所需的时间(以分钟为单位)。但是,没有必要将它创建为对象,变量将处理它,因为我们实际上只需要知道时钟显示的时间。例如,汽车的时钟或驾驶员的手表可以用来确定时间,但我们不考虑这两者,因为我们有自己的确定时间的方法

我们可以为汽车及其速度创建自定义对象。然而,我们在这个物体上唯一能保持的值就是速度。但是,如果我们需要使用不同的时间为多辆车运行代码,我们可以为每辆车创建一个对象。这里不需要,因为只有一辆车。同样,时钟的值可以添加到汽车对象中,但是,同样,我们只需要知道时间,而不需要知道它是否显示在时钟上或时钟/手表是否实际存在

事实

有六种:

1开始时间

2变速1次

3变速2次

4结束时间

我们每分钟都在检查速度

6速度取决于时间

这四次是静态值,因此我们可以创建变量来保存这些值,代码中的任何内容都不会更改这些值。通常,它们是使用
const
创建的,但我在代码示例中使用了
var

事实上,我们每分钟都在检查/更改速度,这正好为我们提供了一个理由,可以精确确定开始时间和结束时间之间的特定时间。需要一个变量来处理此问题,因为当前时间值将在代码运行期间更改

// Create the static time variables
// Start time
var startTime = new Date();
startTime.setHours(10);
startTime.setMinutes(25);
startTime.setSeconds(21);

// Speed change 1 time
var endTime = new Date();
endTime.setHours(12);
endTime.setMinutes(0);
endTime.setSeconds(0);

// Speed change 2 time
var time1 = new Date();
time1.setHours(10);
time1.setMinutes(36);
time1.setSeconds(21);

// End time
var time2 = new Date();
time2.setHours(10);
time2.setMinutes(46);
time2.setSeconds(21);

// Create the other variables
// "thisTime" is initially set to "startTime" but will be incremented by the loop code
var thisTime = startTime;
// A variable to hold the current speed through the loops
// This could be set to 6, but if the loops don't run (ie, the condition is not met), we would not want the speed to be 6
var speed = 0; 

// Start a loop - check that the current time (thisTime) is less than the endTime
while (thisTime < endTime) {
  // If it is, check if we have reached the first speed change time - "time1"
  if (thisTime < time1) {
    // If we haven't, keep the speed at 6
    speed = 6;
    // If we have, check if we have reached the second speed change time - "time2"
  } else if (thisTime < time2) {
    // If we haven't, the speed is 7
    speed = 7;
    // If we have, we can now increase the speed 1mph for each minute (ie, for all further iterations of the loop)
  } else {
    speed++;
  }
  // Output the time and speed
  document.write(thisTime + ":" + speed + "\n<br>");
  // Add one minute to the thisTime variable which will be tested at the start of the next iteration of the loop
  thisTime.setMinutes(thisTime.getMinutes() + 1);
}
声明

有两种初始状态:

1时间从10:25:21开始

我们开始以每小时6英里的速度开车

有一个f
var date = new Date();
date.setHours(10);
date.setMinutes(25);
date.setSeconds(21);

var speed; 
var dateToShow;

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}

for (let i =  1; i < 96; i++) { 
    if (i < 11) { 
        speed = 6; 
    } 
    //increase speed by 1 after 10:36:21
    else if (i < 22) 
    { speed = 7; 
    } 
    // speed continously increase by 1 after 10:46:21
    else
    { speed++; 
    } 
    
    dateToShow = addMinutes(date, i); 

    document.write(dateToShow + ":" + speed + "m/s <br></br>")

}
// Create the static time variables
// Start time
var startTime = new Date();
startTime.setHours(10);
startTime.setMinutes(25);
startTime.setSeconds(21);

// Speed change 1 time
var endTime = new Date();
endTime.setHours(12);
endTime.setMinutes(0);
endTime.setSeconds(0);

// Speed change 2 time
var time1 = new Date();
time1.setHours(10);
time1.setMinutes(36);
time1.setSeconds(21);

// End time
var time2 = new Date();
time2.setHours(10);
time2.setMinutes(46);
time2.setSeconds(21);

// Create the other variables
// "thisTime" is initially set to "startTime" but will be incremented by the loop code
var thisTime = startTime;
// A variable to hold the current speed through the loops
// This could be set to 6, but if the loops don't run (ie, the condition is not met), we would not want the speed to be 6
var speed = 0; 

// Start a loop - check that the current time (thisTime) is less than the endTime
while (thisTime < endTime) {
  // If it is, check if we have reached the first speed change time - "time1"
  if (thisTime < time1) {
    // If we haven't, keep the speed at 6
    speed = 6;
    // If we have, check if we have reached the second speed change time - "time2"
  } else if (thisTime < time2) {
    // If we haven't, the speed is 7
    speed = 7;
    // If we have, we can now increase the speed 1mph for each minute (ie, for all further iterations of the loop)
  } else {
    speed++;
  }
  // Output the time and speed
  document.write(thisTime + ":" + speed + "\n<br>");
  // Add one minute to the thisTime variable which will be tested at the start of the next iteration of the loop
  thisTime.setMinutes(thisTime.getMinutes() + 1);
}