gpt4 book ai didi

for-loop - 代码已编译,但未执行for循环

转载 作者:行者123 更新时间:2023-12-01 22:37:10 25 4
gpt4 key购买 nike

package main

import "fmt"

// I want to make a code that outputs a series of "n" odd numbers.
// For example-> Input : 7 --> Output : 1 , 3 , 5 , 7

func main(){

var n int
var i int

fmt.Println("I'll give you a series of odd numbers to n ") // I'm asking which number do I want the series to stop.

fmt.Scanln(&n)

if n%2 == 1 {

for i=0 ; i >= n; i++ { // Here it's supposed to print out the series but at least something
// The whole code compiles but when I execute it, the program just takes the number that I ask in the Scanln
var odd int // and it stops there. What am I doing wrong? The for cycle?

odd = (i*2) - 1

fmt.Printf(disp)

}

} else {

fmt.Println("It's round") //I've put the for in an (if/else) just to see if it somehow it didn't read until the for part.
//But the (if/else) works but the for cycle doesn't. I'm so confused.
}

}

最佳答案

我认为您的问题在于以下几行:

...
for i=0 ; i >= n; i++ {
...

for循环上的条件应为true才能在内部循环。

但是这里 i从0开始,并且 n始终更高,除非您设置为0。

i的条件更改为小于 n,它将起作用。

关于for-loop - 代码已编译,但未执行for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com