当前位置:网站首页>[C console] - C console class
[C console] - C console class
2022-07-19 08:01:00 【Burning and blowing】
##################################################
Catalog
int.Parse() Convert integer type method
##################################################
Console class
——————————
C# Input and output
Let's talk about C# Input and output of :
Console.WriteLine("Hello,World!");
Console.ReadLine();Remember Java The output statement of I'm already familiar with it :
System.out.print();
System.out.printf();
System.out.println(); Similar structures Console yes C# Console class in
utilize Console Class can be very convenient for console Input / Output
There are two common output methods :
Console.WriteLine(); /* Console . Write a line (); */
Console.Write(); /* Console . Write (); */
You can see that ?
The only difference is to add Line After the output, the line will wrap !
What about the input method :
Console.Read();
Console.ReadLine();Read a line This method returns string type
——————————
C# Output to console
We have three ways to output to the console :
Console.WriteLine();
Console.WriteLine( The value to output );
Console.WriteLine(" Format string ", Variable list );The first two are related to Java Medium
System.out.println();The usage is the same Just try it yourself
The third way Is the most popular one ! Place holder
In this way C、Java、C# Chinese are very popular !
for instance :
string course = "C#";
Console.WriteLine(" I'm learning >>> {0}", course);When you run, it will output :
I'm learning >>> C#In this form WriteLine() The parameter of consists of two parts :
(" Format string ", Variable list )
Format string by :
" I'm learning >>> {0}"and
{0}be called Place holder What position does it occupy ? Occupy is the back
course
The position of the variable
We are Format string In turn use
{0}
{1}
{2}
{N}
Represents the variable to be output Then arrange the variables in the variable list in turn
{0} Corresponding to the first variable {1} Corresponding to the next second variable And so on
This way is much more convenient than using a plus sign :
Console.WriteLine(name + ", " + course + ", " + age);
Console.WriteLine("{0}, {1}, {2}", name, course, age); Because when you connect with a plus sign Easy to make mistakes and troublesome
Placeholder mode is easy to maintain !
——————————
C# Read... From the console
And output Console.WriteLine() Corresponding Input can be used Console.ReadLine() Method
Write It means to write Read It means reading
Console.ReadLine() Returns a string We can assign it directly to a string variable !
code:
string name = Console.ReadLine();%%%%%
int.Parse() Convert integer type method
There's another magical use ! That is to say If we want to input and save plastic data What should I do ?
It's simple Convert the returned string into an integer !
code:
int age = int.Parse(Console.ReadLine());Remember the concept of data conversion ? int.Parse() Is the method of converting strings to integers
——————————
The sample program
Write a small project to get familiar with Console input and output user information
VSCode code:
using System;
namespace _2. Place holder _ Console read in assignment conversion _ Output user information
{
class Program
{
static void Main(string[] args)
{
/* demonstration
*
* Console I / O
* Place holder
* Console read
* Console read in assignment conversion
*/
/* Program preparation */
Console.Write(" Please enter a user name <<< ");
string name = Console.ReadLine(); // The program executes here and waits for the user to input the string assigned to name Variable
Console.Write(" Please enter the user level <<< ");
int level = int.Parse(Console.ReadLine()); // Program execution to the number of input string It has to be numbers Then it is transformed into shaping data and assigned to level
/* Program output */
Console.WriteLine();
Console.WriteLine("##################################################");
Console.WriteLine(name + " Hello ! Your rank is " + level); // Plus sign connection output
Console.WriteLine("{0} Hello ! Your rank is {1}", name, level); // Placeholder output
/* Program end */
Console.ReadLine();
}
}
}VSCode demo:
Windows PowerShell
copyright (C) 2014 Microsoft Corporation. All rights reserved .
PS E:\C#> cd "e:\C#\" ; if ($?) { dotnet run Program.cs }
E:\C#\Program.cs(19,27): warning CS8600: take null Text or possible null The value is converted to a value that cannot be null type . [E:\C#\C#.csproj]
E:\C#\Program.cs(21,35): warning CS8604: “int int.Parse(string s)” Parameter in “s” It's possible that null Reference arguments . [E:\C#\C#.csproj]
Please enter a user name <<< Li erha
Please enter the user level <<< 1314
##################################################
Li erha Hello ! Your rank is 1314
Li erha Hello ! Your rank is 1314
PS E:\C#>Although there is a warning message at compile time But it does not affect the operation of the program
边栏推荐
- Xilinx ultrascale+ MPSoC (zu9eg/zu15eg) high performance PCIe data preprocessing board
- Jd.com's purchase intention forecast (IV)
- 4-channel fmc+ baseband signal processing board (4-channel 2G instantaneous bandwidth ad+da)
- Xinlinx zynq7010 domestic replacement fmql10s400 national production arm core board + expansion board
- Shenzhen Prudential written examination record
- cad填充转多段线脚本
- 自用的一套Jenkins样式
- VMware Cloud Director 10.4 发布 (含下载) - 云计算调配和管理平台
- 【MySQL】 事务:事务基础知识、MySQL事务实现原理、事务日志 redolog & undolog 详解
- Beijing Jiewen technology, an acquiring outsourcing service provider, transferred 60% of its shares for about 480million
猜你喜欢

Forecast sales xgboost

Introduction & use of Maxwell

Spark3.x entry to mastery - stage 7 (spark dynamic resource allocation)

1669. Merge two linked lists (merge of two linked lists)

CCF-CSP《202206-2—寻宝!大冒险!》

912. 排序数组(数组排序)

Is there any cumulative error in serial communication and the requirements for clock accuracy

FMC sub card: 8-channel 125msps sampling rate 16 bit AD acquisition sub card
![Conversation technology [dark horse introduction series]](/img/31/ba806652a2a836e53c7fa942770696.png)
Conversation technology [dark horse introduction series]

Modify radio style
随机推荐
Flink introduction to practice - phase II (illustrated runtime architecture)
4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)
csp-2020-6-《角色授权》
Facial key point detection CNN
收单外包服务商北京捷文科技以约4.8亿转让60%股份
分叉币的发展史及价值|ETH、BCH、BSV 2020-03-08
Modify scroll bar style
Kingbasees can realize any of MySQL by constructing an aggregate function_ Value function.
[day01] preface, introductory program, constant variables
怎么检查APP是否存在用户信息数据泄露漏洞
数据库复习--数据库恢复技术
看完代码回首看论文:YOLOv3重读
Discussion sur la technologie RISC - V
1669. Merge two linked lists (merge of two linked lists)
VMware Cloud Director 10.4 发布 (含下载) - 云计算调配和管理平台
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
@How can conditionalonmissingbean cover beans in third-party components
深圳保诚笔试记录
Redis 跳跃表实现原理 & 时间复杂度分析
Virtual machine stack of [JVM]