C#中console.read()和console.readline()

简介: C#中console.read()和console.readline()

现在的学到已经到了C#中,为什么会提出题目中特别常见的方法呢?请看下面这段代码。

static void Main(string[] args)
        {
            Console.WriteLine("请输入一个年份");
            string str = Console.ReadLine();
            int yearInt = Convert.ToInt32(str);//报异常
            //int yearInt=outputNumber(Console.ReadLine());
            bool result = Isyear(yearInt);
            if (result)
            {
                Console.WriteLine("闰年");
            }
            else
            {
                Console.WriteLine("平年");
            }
            Console.ReadKey();
        }


这段代码是输入一个年份判断是闰年合适平年,对于这个类型的代码相信大家都可以很容易的写出来,但是对于我这种小白来说,一些细节性的问题就会暴露出来,比如在第4行代码中为什么要用string呢?我的第一感觉应该是用int的呀,因为年份不都是一串数字的吗?于是我尝试着把string改为int,很好!它居然报错了,既然错了就要找找为什么int就是错的呢?它给的提示是这样式儿的。“无法将string类型强制转换为int型”,但是看上面的代码也没有定义string类型,于是上网查资料,才发现不是类型的问题,而是console.readline()的问题。在代码“string str=console.readline()”中,用到了console.readline,而这种函数读取的是一行字符,返回string类型,所以在他的“=”前面才会定义为string类型的。

那么,console.read()是怎么回事呢?console.read()函数读取一个字符,返回int型。


以上是对这段代码的理解,如果谬误,欢迎斧正!


相关文章
|
6天前
|
存储 安全 Unix
/var/log/messages说明
【4月更文挑战第5天】`/var/log/messages`是类Unix系统(包括Linux)的日志文件,存储系统启动、关闭、内核消息、服务状态和服务错误等信息。内容可能因配置和日志系统(如syslog、rsyslog、journald)不同而变化。现代Linux可能使用`journalctl`等工具访问journald日志,提供更强大管理功能。
45 1
/var/log/messages说明
|
6天前
报错:cannot read properties of undefined(reading ‘forEach‘)
报错:cannot read properties of undefined(reading ‘forEach‘)
报错:cannot read properties of undefined(reading ‘forEach‘)
|
6天前
报错:cannot read poperties of undefined(reading ‘then‘)
报错:cannot read poperties of undefined(reading ‘then‘)
|
6天前
|
JavaScript
【报错】:Cannot read properties of undefined (reading ‘prototype‘)
【报错】:Cannot read properties of undefined (reading ‘prototype‘)
176 0
|
6天前
|
Web App开发 前端开发 JavaScript
console importer
console importer
126 0
|
7月前
|
编译器 C语言
__FILE__, __LINE__ __FUNCTION__
__FILE__, __LINE__ __FUNCTION__
|
10月前
|
C#
C#中Console.WriteLine()与Console.Write的区别
C#中Console.WriteLine()与Console.Write的区别
68 0
|
9月前
|
XML JavaScript 数据格式
JS console.log、console.dir、console.dirxml 区别
JS console.log、console.dir、console.dirxml 区别
41 0
|
JavaScript C语言
console.log(a + a++ * ++ a)到底输出什么?
前言 有些小伙伴可能看到这道题目就已经蒙圈了!甚至可能看不懂这道题目在干什么。其实这是一道比较考察基础的面试题,当你明白原理之后,你可能会感觉这道题也就那么回事,但是如果你没有思绪,那你可能觉得这道题很难。 今天我们就来彻底看看这到底在做什么妖!
462 0
console.log(a + a++ * ++ a)到底输出什么?
why debug log could not be displayed in console
Created by Wang, Jerry on Mar 07, 2016
87 0
why debug log could not be displayed in console