如何循环遍历包含多个数字的行的文本文件,同时对数字进行计数

该文件看起来像:


John Smith

100 90 80 90

50 60 80 99 40 20

但文件中可以有任意数量的人员/等级。我知道如何循环并获取该人的名字和姓氏,但是如何循环第一行数字,将它们添加到自己的总计中,然后循环第二行并将它们添加到另一个总计中?


我还没有找到一种方法来检查Go中的行尾,所以我不知道如何区分第一行数字和第二行数字。


这是我尝试过的:


package main


import (

    "fmt" 

    "os"

    "log"

    "bufio"

    //"unicode"

    //"container/list"

)


type Student struct {

    FirstName string

    LastName string

}



func main(){

    fmt.Println("What is the name of your file?\n")

    var filename string

    fmt.Scan(&filename)


    file, err := os.Open(filename)

    if err != nil {

        log.Fatal(err)

    }

    scanner := bufio.NewScanner(file)

    scanner.Split(bufio.ScanWords)

    //var scanCount int = 0

    //var studentCount = 1

    //var gradeSum = 0

    //var gradeAvg = 0


    var students [100]Student


    for scanner.Scan() {

        students[0].FirstName = scanner.Text()

        students[0].LastName = scanner.Text()

        fmt.Println(students[0].FirstName)


        //count ++


    }


}

这是我到目前为止所拥有的。我尝试过的所有涉及数字的方法都不起作用,所以我将其删除了。


陪伴而非守候
浏览 151回答 1
1回答

慕标琳琳

最简单的方法是按行循环遍历文件。像这样的东西:package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings")type Student struct {&nbsp; &nbsp; FirstName string&nbsp; &nbsp; LastName&nbsp; string}func main() {&nbsp; &nbsp; fmt.Println("What is the name of your file?\n") var filename string&nbsp;&nbsp; &nbsp; fmt.Scan(&filename)&nbsp; &nbsp; file, err := os.Open(filename)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp;log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; scanner := bufio.NewScanner(file)&nbsp; &nbsp; for scanner.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; line := scanner.Text()&nbsp; &nbsp; &nbsp; &nbsp; if len(line) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // skip blank lines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if '0' <= line[0] && line[0] <= '9' {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum := 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _, field := range strings.Fields(line) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n, err := strconv.Atoi(field)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(sum)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fields := strings.Fields(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(fields) != 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal("don't know how to get first name last name")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("First:", fields[0], "Last:", fields[1])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if err := scanner.Err(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }}在游乐场上看到它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go