dongwen3437 2016-02-13 22:06
浏览 56
已采纳

Go-为什么我的For Loop会抛出“意外的分号或换行符”?

I'm writing a dice rolling function. In order to add the result of each die, I added to an output variable using a for loop. However, I'm getting an error thrown when I attempt to build;

syntax error: unexpected semicolon or newline, expecting {

This was thrown on the line initializing the for loop. Here is my code:

    for i := 0; i < [0]si; i++ {
        output := output + mt.Intn([1]si)
    }

si is simply an int array holding 2 values, and mt is the name I gave to math/rand when I imported it.

  • 写回答

2条回答 默认 最新

  • dongqi3533 2016-02-13 22:14
    关注

    Your loop has several problems:

    1. The use of square brackets is odd. Outside of type definitions, these go after slice/array names, e.g. x[i] would give you the ith element of the slice x.
    2. There is no reference to i inside the loop body, and thus each iteration of the loop will do the same thing.
    3. You should probably write output = output + ... without the colon. Otherwise, a new variable output is declared during each iteration of the loop, and forgotten immediately after so that the loop has no effect.

    The compiler error is probably caused by the first of these problems.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据