如何去掉打印行末尾的百分号字符?

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

How do I get rid of the % character at the end of the printed line?

问题

package main
import "fmt"

func main() {
	var scores = []int {90, 70, 50, 80, 60, 85}
	var length = len(scores) // len函数返回scores的长度
	for i := 0; i<length; i++ {
		fmt.Printf("%d ", scores[i]) // 输出:90 70 50 80 60 85 %
	}
}

// 输出:90 70 50 80 60 85 %
// 注意:我如何去掉打印行末尾的%符号?

英文:
package main
import &quot;fmt&quot;

func main() {
	var scores = []int {90, 70, 50, 80, 60, 85}
	var length = len(scores) // the len function returns the length of scores
	for i := 0; i&lt;length; i++ {
		fmt.Printf(&quot;%d &quot;, scores[i]) // Output: 90 70 50 80 60 85 %
	}
}

// Output: 90 70 50 80 60 85 %
// Note: How do I get rid of the % at the end of the printed line?

答案1

得分: 3

你的程序没有打印出%,这是因为在程序结束后,你的shell会在新的一行开始。

为了正确分隔你的输出,可以在循环结束后添加fmt.Println(""),确保在输出后有一个换行符。

英文:

Your program does not print a %, this is your shell starting a line after the program ends.

To separate your output properly, add a fmt.Println(&quot;&quot;) after your loop, ensuring that there is a newline after your output.

huangapple
  • 本文由 发表于 2022年1月31日 05:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/70919061.html
匿名

发表评论

匿名网友

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

确定