package main import (
"flag"
"fmt"
"math"
"os"
) /*
go build xxx.go 编译产生同名的exe文件(在当前文件夹下)
go run xxx.go 编译执行二合一(产生的exe文件在临时目录下)
go builde -o yoursister.exe xxx.go 编译产生yoursister.exe文件(在当前文件夹下)
*/ /*简易获取命令行参数:获取命令行上输入的所有字符串*/
func main031() {
fmt.Println(os.Args) //获取rmb参数
for i,arg := range os.Args{
if arg == "rmb"{
fmt.Println("rmb=",os.Args[i+1])
break
}
}
} //yoursister -name 如花 -age 18 -rmb 0.5 -married true
func main032() { //定义一个字符串类型的命令行参数:参数名称name,参数默认值“无名氏”,作用“你妹芳名”
//得到一个将来存储name参数的值的地址/指针
namePtr := flag.String("name","无名氏","你妹芳名") //定义了整型命令行参数age
agePtr := flag.Int("age", 0, "你妹芳龄") //定义浮点型命令行参数rmb
rmbPtr := flag.Float64("rmb", 0, "你妹资产") //定义布尔型命令行参数married
marriedPtr := flag.Bool("married", false, "你妹婚否") //解析获取参数值
flag.Parse() fmt.Println(*namePtr,*agePtr,*rmbPtr,*marriedPtr) //根据你妹的具体信息求得你妹的受欢迎指数
//...
} //yoursister -name 如花 -age 18 -rmb 0.5 -married true
func main() {
var name string//"" 有地址
var age int//0
var rmb float64
var married bool /*定义命令行参数*/
flag.StringVar(&name,"name","无名氏","你妹芳名")
flag.IntVar(&age,"age",0,"你妹芳龄")
flag.Float64Var(&rmb,"rmb",0,"你妹资产")
flag.BoolVar(&married,"married",false,"你妹婚否") //解析获取参数值
flag.Parse()
fmt.Println(name,age,rmb,married) //评估你妹的受欢迎程度
popularity := GetPopularity(name, age, rmb, married)
fmt.Printf("你妹的受欢迎程度是%d%%\n",popularity)
} /*评估某人的受欢迎程度*/
func GetPopularity(name string,age int,rmb float64,married bool) int{ //预设受欢迎程度为60
var popularity float64 = 60.0 //如果名字是四个字以上,受欢迎程度*=1.1
if len(name) >= 3*4{
popularity *= 1.1
} //越有钱越受欢迎(以5万为60分,每增加1万,魅力值+=1)
popularity += (rmb-50000)/10000 //越年轻越受欢迎
popularity *= (35.0/float64(age)) //如果已婚,受欢迎程度*=0.5
if married {
popularity *= 0.5
} return int(math.Round(popularity))
} func test() {
//fmt.Println(len("西门如花"))
fmt.Println(GetPopularity("西门耶律不花",20,50000,false))
fmt.Println(GetPopularity("翠花",80,0.5,true))
}