为什么我的for循环不访问该对象内的数组?

如何解决为什么我的for循环不访问该对象内的数组?

我正在尝试绘制橙色椭圆,但是代码底部的for循环未绘制任何内容。我知道下面的代码是访问对象内部数组的正确方法,因为我在另一个任务中使用了它并且它可以工作。我以前有beginShape();和endShape();在我的for循环之前和之后,但这也不起作用。

一切看起来都正确,我不确定自己在做什么错。谢谢大家的帮助。谢谢

Case 601 - Cross Reference - stage 2

Fry is still on the loose. We think she’s resorted to stealing to get by.
Hopefully we can track her down by cross-referencing sightings and recent thefts in the area.

In the setup function,use a for loop to traverse the sightings,marking all of the locations on the map
where she was last seen. Do this by drawing small,DarkOrange stroke ellipses at each location.

In addition,we've assembled a list of recent thefts in the area. Using another for loop to traverse the
recent crime records,you should mark those locations on the map. Do this by drawing small,Magenta stroke rectangles centered over each location.

Use X11 colours. You can find a reference table at https://en.wikipedia.org/wiki/Web_colors.

For this mission you will need ONLY the following:

- for loop
- stroke
- ellipse()

- stroke
- rect() NB. Draw each rectangle with the point at its center.


*/

var countyMap;

//Sightings of Casey Fry.

var absconder_record = {
    Loc_X: [639,681,712,756,715,701,753,815,795,788,781,768,750,732,714,695,693,654,624,594,555],Loc_Y: [288,286,293,310,368,425,436,468,506,497,486,489,500,514,531,552,523,484,474],};

//Recent crime records.

var robbery_record = [ 
  { PointX : 403,PointY : 401},{ PointX : 402,PointY : 360},{ PointX : 427,PointY : 403},{ PointX : 646,PointY : 284},{ PointX : 639,PointY : 264},{ PointX : 830,PointY : 434},{ PointX : 809,PointY : 443},{ PointX : 844,PointY : 496},{ PointX : 802,PointY : 350},{ PointX : 683,PointY : 413},{ PointX : 552,PointY : 464},{ PointX : 629,PointY : 498},{ PointX : 712,PointY : 562},{ PointX : 783,PointY : 603},{ PointX : 415,PointY : 225},{ PointX : 561,PointY : 282},{ PointX : 562,PointY : 392},{ PointX : 751,PointY : 283},{ PointX : 680,PointY : 359},{ PointX : 626,PointY : 436},{ PointX : 701,PointY : 455},{ PointX : 838,PointY : 565},{ PointX : 322,PointY : 508},{ PointX : 468,PointY : 556},{ PointX : 625,PointY : 737} 
];


function preload()
{
    countyMap = loadImage("map.png")
}

function setup()
{
  createCanvas(countyMap.width,countyMap.height);

    image(countyMap,0);

    //add your code below here
    stroke(255,140,0);
    
    for(i = 0; i <absconder_record.Loc_X[i].length; i++ ){
        ellipse(absconder_record.Loc_X[i],absconder_record.Loc_Y[i],5)
    }
    
    
    


}

以下是其应绘制的图像:

enter image description here

解决方法

看着i <absconder_record.Loc_X[i].length

  • absconder_record.Loc_X是一个数组

  • absconder_record.Loc_X[i]是一个数字

  • absconder_record.Loc_X[i].length是数字length的{​​{1}}属性,该数字是undefined

  • 对小于undefined的数字进行比较检查将返回false

因此循环条件总是失败,并且循环体永远不会执行。

,

查看此行:

 for(i = 0; i <absconder_record.Loc_X[i].length; i++ ){

我认为您的意思是:

i <absconder_record.Loc_X.length


i <absconder_record.Loc_X[i].length

  • i为0
  • absconder_record.Loc_X[0]639
  • 639没有length属性。

关注评论中的问题:

如何遍历数组robbery_record?

for loop包含3个表达式:

  1. 初始化程序,通常用于初始化计数器,如i = 0;中所示。 (这只运行一次,而不是每次迭代。)
  2. 要测试的条件。如果条件为真,则循环主体运行。最常见的用途是查看计数器是否已超过某个阈值,例如i > 10。如果i小于10,请运行循环的正文。
  3. 在循环的每次迭代之后运行的表达式。最常用的递增计数器:i++

在一起看起来像这样:

for (let i = 0; i < 10; i++) {
  // body
}
  1. i0开头。
  2. 评估条件:我是否小于10?
  3. 如果是这样,循环的主体就会运行。
  4. 正文运行后,将执行最后一个表达式(i++)。现在我是1。
  5. 重复2-4,直到条件不再成立为止(当我不小于10时)。

要使用for循环遍历数组,通常的方法与上述方法完全相同,但是将10替换为数组的长度。因此,在您的情况下使用robbery_record:

for (let i = 0; i < robbery_record.length; i++) {
  // do stuff
}

通常,当有人这样做时,是因为他们想对数组中的每个项目执行一次主体。方便地,由于i每次在循环中都会递增,因此您可以通过robbery_record[i]在循环体内获取当前项:

for (let i = 0; i < robbery_record.length; i++) {
  const record = robbery_record[i];
  // do stuff with the current record
}

每个robbery_record条目都是一个具有PointXPointY属性的对象,因此您可以在循环体中使用它们:

for (let i = 0; i < robbery_record.length; i++) {
  // record is an object,like: { PointX: 123,PointY: 456 }
  const record = robbery_record[i];

  // do stuff with the object's properties
  console.log(record.PointX) // 123
  console.log(record.PointY) // 456

  const x = record.PointX; // now x === 123
  const y = record.PointX; // now y === 456

  // etc.
}

希望这会有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-