more than one character in rune literal in go

huangapple go评论91阅读模式
英文:

more than one character in rune literal in go

问题

我是新手。我的问题是获取奇数。但是我遇到了这个错误。我不知道你能否解释一下我犯了什么错误?

package main
import "fmt"

func main(){
	var a int 
	fmt.Printf("输入一个数字:")
	fmt.Scanf("%d", &a)

	if a % 2==0{
		fmt.Println("%d 是偶数", a)
	}else{
		fmt.Println("%d 是奇数", a)
	}
}

错误是在 fmt.Printffmt.Println 函数中的格式化字符串中使用了错误的占位符。正确的占位符应该是 %v 而不是 %d。所以你需要将 fmt.Printffmt.Println 函数中的 %d 替换为 %v。这样你的代码就能正确地打印出奇数或偶数了。

英文:

I am the new to go. My problem is get the odd number. But I get this error. I don't know can you explain this what mistake I am done?

package main
import "fmt"

func main(){
	var a int 
	fmt.Printf("Enter the number : ")
	fmt.Scanf('%d', &a)

	if a % 2==0{
		fmt.Println(" %d Is even number", a)
	}else{
		fmt.Println( "%d is odd number", a)
	}
}

答案1

得分: 3

'%d'更改为"%d"

在Go中,单引号用于rune字面量

'a'
'ä'
'本'
'\t'
'
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\''         // 包含单引号字符的rune字面量
'aa'         // 非法:字符过多
'\xa'        // 非法:十六进制数字过少
'\0'         // 非法:八进制数字过少
'\uDFFF'     // 非法:代理半个
'\U00110000' // 非法:无效的Unicode代码点
0' '
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\''         // 包含单引号字符的rune字面量
'aa'         // 非法:字符过多
'\xa'        // 非法:十六进制数字过少
'\0'         // 非法:八进制数字过少
'\uDFFF'     // 非法:代理半个
'\U00110000' // 非法:无效的Unicode代码点
7' '7' '\x07' '\xff' '\u12e4' '\U00101234' '\'' // 包含单引号字符的rune字面量 'aa' // 非法:字符过多 '\xa' // 非法:十六进制数字过少 '
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\''         // 包含单引号字符的rune字面量
'aa'         // 非法:字符过多
'\xa'        // 非法:十六进制数字过少
'\0'         // 非法:八进制数字过少
'\uDFFF'     // 非法:代理半个
'\U00110000' // 非法:无效的Unicode代码点
' // 非法:八进制数字过少 '\uDFFF' // 非法:代理半个 '\U00110000' // 非法:无效的Unicode代码点

双引号和反引号用于字符串字面量

`abc`                // 与"abc"相同
`\n\n`                  // 与"\\n\n\\n"相同
"\n"
"\""                 // 与`"`相同
"Hello, world!\n"
"日本語"
"\u65e5本\U00008a9e"
"\xff\u00FF"
"\uD800"             // 非法:代理半个
"\U00110000"         // 非法:无效的Unicode代码点
英文:

Change '%d' to "%d".

In Go single quotes are used for rune literals.

'a'
'ä'
'本'
'\t'
'
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\''         // rune literal containing single quote character
'aa'         // illegal: too many characters
'\xa'        // illegal: too few hexadecimal digits
'\0'         // illegal: too few octal digits
'\uDFFF'     // illegal: surrogate half
'\U00110000' // illegal: invalid Unicode code point
0' '
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\''         // rune literal containing single quote character
'aa'         // illegal: too many characters
'\xa'        // illegal: too few hexadecimal digits
'\0'         // illegal: too few octal digits
'\uDFFF'     // illegal: surrogate half
'\U00110000' // illegal: invalid Unicode code point
7' '7' '\x07' '\xff' '\u12e4' '\U00101234' '\'' // rune literal containing single quote character 'aa' // illegal: too many characters '\xa' // illegal: too few hexadecimal digits '
'a'
'ä'
'本'
'\t'
'\000'
'\007'
'\377'
'\x07'
'\xff'
'\u12e4'
'\U00101234'
'\''         // rune literal containing single quote character
'aa'         // illegal: too many characters
'\xa'        // illegal: too few hexadecimal digits
'\0'         // illegal: too few octal digits
'\uDFFF'     // illegal: surrogate half
'\U00110000' // illegal: invalid Unicode code point
' // illegal: too few octal digits '\uDFFF' // illegal: surrogate half '\U00110000' // illegal: invalid Unicode code point

Double quotes and back quotes are used for string literals.

`abc`                // same as "abc"
`\n
\n`                  // same as "\\n\n\\n"
"\n"
"\""                 // same as `"`
"Hello, world!\n"
"日本語"
"\u65e5本\U00008a9e"
"\xff\u00FF"
"\uD800"             // illegal: surrogate half
"\U00110000"         // illegal: invalid Unicode code point

huangapple
  • 本文由 发表于 2021年7月20日 10:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/68448820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定