dongyan1974 2018-11-09 21:20
浏览 101
已采纳

可以在Linux上正常运行程序,但不能在Windows上运行

I am currently learning Go lang. Trying it on different platforms: Linux, Windows When I run code on Linux it runs perfectly, but when I try this program on Windows it doesn't work.

Its just simple cmd calculator which allows simple operations like add number, multiply eg. Its not handling wrong input like characters. It's my first program for adoption Go syntax

What doesn't work:

  1. Parsing int
  2. Comparing input

Code:

package main

import (
    "bufio"
    "fmt"
    "math"
    "os"
    "strconv"
    "strings"
)

func main() {

    reader := bufio.NewReader(os.Stdin)
    var operation int
    var firstNumber float64
    var secondNumber float64

    fmt.Println("Simple cmd calculator")

    repeat := true

    for repeat {

        fmt.Println("Enter number 1: ")
        firstNumber = getNumber(*reader)

        fmt.Println("Enter number 2: ")
        secondNumber = getNumber(*reader)

        fmt.Println()

        selectOperation(*reader, &operation)

        fmt.Print("You result is: ")

        switch operation {
        case 1:
            fmt.Println(add(firstNumber, secondNumber))
        case 2:
            fmt.Println(subtract(firstNumber, secondNumber))
        case 3:
            fmt.Println(multiply(firstNumber, secondNumber))
        case 4:
            fmt.Println(divide(firstNumber, secondNumber))
        }

        fmt.Println("Do you want to continue? [Y/n]")
        input, _ := reader.ReadString('
')

        input = strings.Replace(input, "
", "", -1)

        if !(input == "Y" || input == "y") {
            repeat = false
        }

    }

}

func selectOperation(reader bufio.Reader, operation *int) {
    fmt.Println("1. Add")
    fmt.Println("2. Subtract")
    fmt.Println("3. Multiply")
    fmt.Println("4. Divide")

    fmt.Print("Select operation: ")
    input, _ := reader.ReadString('
')
    input = strings.Replace(input, "
", "", -1)
    number, _ := strconv.Atoi(input)
    *operation = number
}

func getNumber(reader bufio.Reader) float64 {

    input, _ := reader.ReadString('
')
    input = strings.Replace(input, "
", "", -1)
    convertedNumber, _ := strconv.ParseFloat(input, 64)
    return convertedNumber

}

func add(a float64, b float64) float64 {
    return (math.Round((a+b)*100) / 100)
}

func subtract(a float64, b float64) float64 {
    return (math.Round((a-b)*100) / 100)
}

func multiply(a float64, b float64) float64 {
    return (math.Round(a*b*100) / 100)
}

func divide(a float64, b float64) float64 {
    return (math.Round(a/b*100) / 100)
}

Results:

Linux

Windows

Am I doing something wrong or it's not my bad?

  • 写回答

1条回答 默认 最新

  • doufen3786 2018-11-09 21:50
    关注

    Thanks for help from @zerkms.

    Answer is:

    input = strings.Replace(input, "", "", -1)
    input = strings.Replace(input, "
    ", "", -1)
    

    Now it will work properly both on windows and linux

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失