zoukankan      html  css  js  c++  java
  • 5.1 标准输入 NewScanner

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	var name string
    	fmt.Println("What is your name?")
    	fmt.Scanf("%s
    ", &name)
    
    	var age int
    	fmt.Println("What is your age?")
    	fmt.Scanf("%d
    ", &age)
    
    	fmt.Printf("Hello %s, your age is %d
    ", name, age)
    
    }
    
    /*
    What is your name?
    dsf
    What is your age?
    11
    Hello dsf, your age is 11
    */
    
    
    
    package main
    
    import (
    	"bufio"
    	"fmt"
    	"os"
    )
    
    func main() {
    
    	// The Scanner is able to
    	// scan input by lines
    	sc := bufio.NewScanner(os.Stdin)
    	for sc.Scan() {
    		txt := sc.Text()
    		fmt.Printf("Echo: %s
    ", txt)
    	}
    
    }
    
    /*
    hh
    Echo: hh
    hh
    Echo: hh
    jhbhj
    Echo: jhbhj
    
    一直捕捉屏幕输入 然后输出
    */
    
    
    
    package main
    
    import (
    	"fmt"
    	"os"
    )
    
    func main() {
    
    	for {
    		data := make([]byte, 8)
    		n, err := os.Stdin.Read(data)
    		if err == nil && n > 0 {
    			process(data)
    		} else {
    			break
    		}
    	}
    
    }
    
    func process(data []byte) {
    	fmt.Printf("Received: %X 	%s
    ", data, string(data))
    }
    
    /*
    h
    Received: 680A000000000000 	h
    ������
    jj
    Received: 6A6A0A0000000000 	jj
    �����
    klk里面了昆明
    Received: 6B6C6BE9878CE99D 	klk里�
    Received: A2E4BA86E69886E6 	�了昆�
    Received: 988E0A0000000000 	��
    �����
    可能离开
    Received: E58FAFE883BDE7A6 	可能�
    Received: BBE5BC800A000000 	�开
    ���
    jk
    Received: 6A6B0A0000000000 	jk
    �����
    mm
    Received: 6D6D0A0000000000 	mm
    �����
    */
    
    
  • 相关阅读:
    STL(1) 指针迭代器
    不错我博主
    C++ 学习书目
    写给VC++ Windows开发的初学者 一片不错的博文
    算法:C语言实现 (4)队列的数组实现
    算法:C语言实现 (4)下推栈的数组实现
    算法:C语言实现 (4)下推栈的链表实现
    vs 使用笔记
    自定义组合控件,适配器原理-Day31
    Android30-Fragment-理解
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620911.html
Copyright © 2011-2022 走看看