例如,我不想要这行代码:Console.Writeline(" \|\/ \| /");干扰程序,因为目前我收到错误无法识别的转义序列,但我不想使用转义序列,我只想让这些字符本身出现在控制台上.有没有办法做到这一点?感谢您抽出宝贵时间阅读.
经过多年的C++工作,我现在正在教自己C#.毋庸置疑,我需要重新学习很多东西.我的问题如下:
Windows控制台应用程序中打印(写入控制台窗口)复杂信息的最佳方法是什么,涉及多个变量,文本字符串和换行符?
更明确地说,我如何将类似以下C++代码的内容翻译成C#:
int x = 1; int y = 2; double a = 5.4;
cout << "I have " << x << " item(s), and need you to give" << endl
<< "me " << y << " item(s). This will cost you " << a << endl
<< "units." << endl;
Run Code Online (Sandbox Code Playgroud)
在C++中,这会将以下内容打印到控制台:
我有1件商品,需要你给
给我2件商品.这将花费你5.4
单位.
这个例子完全是武断的,但我的程序中经常会有这样简短而复杂的消息.我是否完全不得不重新学习如何格式化输出,或者我还没有找到C#中的类似功能(我已经挖掘了很多Console.IO文档).
感谢您查看我的问题,以验证我的意思
Console.WriteLine($"Hello {variable}");
Run Code Online (Sandbox Code Playgroud)
我很好奇$对Console.WriteLine输出的影响
简单来说,我只需在Web应用程序中添加一行:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Console.WriteLine("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
我想要的是读"Hello,world!" 在Visual Studio 2010的输出窗口上,但失败了,有什么不对?

我尝试过:通过工具>选项>项目和解决方案>构建和运行来更改详细程度,并更改"MSBuild项目构建输出详细程度"的值,但没有任何影响.
我正在尝试创建一个C#for循环,它将循环遍历项目数组并打印用户的第一个日期.下面是数组的代码(我认为是正确的).
string[,] arrHolidays = new string[2, 3];
arrHolidays[0, 0] = "10/05/2015";
arrHolidays[0, 1] = "15/05/2015";
arrHolidays[0, 2] = "Danny";
arrHolidays[1, 0] = "20/05/2015";
arrHolidays[1, 1] = "22/05/2015";
arrHolidays[1, 2] = "Kieran";
Run Code Online (Sandbox Code Playgroud)
下面是For循环的代码(抛出错误)
for(int i = 0; i < arrHolidays.Length; i++)
{
Console.WriteLine(arrHolidays[i, 0]);
}
Run Code Online (Sandbox Code Playgroud)
索引超出范围异常未得到处理.这是我收到的错误.当它抛出错误并且我检查值1时它是2,如果这是解决它的任何帮助.我希望在控制台应用程序中看到的是:
10/05/2015
20/05/2015
INB4我几乎没有任何c#经验这是我的第一个项目,所以请解释任何帮助:)
我正在尝试打印各种foreach循环的ArrayList的内容,但我唯一得到的是String + System.Collections.ArrayList.
例如,以下代码:
ArrayList nodeList = new ArrayList();
foreach (EA.Element element in elementsCol)
{
if ((element.Type == "Class") || (element.Type == "Component") || (element.Type == "Package"))
{
nodeList.Add(element);
}
Console.WriteLine("The nodes of MDG are:" + nodeList); //stampato a schermo la lista dei nodi nel MDG finale
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
The nodes of MDG are:System.Collections.ArrayList
请有人告诉我为什么?
鉴于代码:
// person.cs
using System;
// #if false
class Person
{
private string myName = "N/A";
private int myAge = 0;
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
// Declare an Age property of type int:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
public override string ToString()
{
return "Name = " + Name …Run Code Online (Sandbox Code Playgroud) 我正在做一个简单的例子,根据输入键我在控制台上写了不同的消息.问题是每次我读到一个键时,下一个Console.WriteLine()结果都会在开始时连接一个'a'符号?这是一个例子:
ConsoleKeyInfo keyInfor = Console.ReadKey();
if (keyInfor.Key == ConsoleKey.UpArrow)
{
Console.WriteLine("Up arrow");
}
Run Code Online (Sandbox Code Playgroud)
单击向上箭头时的预期结果:"向上箭头"
实际结果:"aUp arrow"
这是一段正在处理的代码
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number to find factorial : ");
var fac = Convert.ToInt32(Console.ReadLine());
var res = 1;
for (var i = fac; i >= 1; i--)
{
res = res * i;
}
Console.WriteLine("Count of " + fac + " is : " + res);
}
}
Run Code Online (Sandbox Code Playgroud)
我想fac在结果中添加用户输入的输入。这是Console.WriteLine("Count of " + fac + " is : " + res);用来在我的控制台中显示输出。
我尝试了类似的东西
Console.WriteLine("Count of {0} is : {1} ", fac, …Run Code Online (Sandbox Code Playgroud) 我经常写一个 Console.WriteLine 语句,然后在稍后阶段修改它。然后我最终得到这样的陈述。
Console.WriteLine("{2} is not greater than {0} or smaller than {1}",min,max, grade);
Run Code Online (Sandbox Code Playgroud)
有没有办法显式命名传递给 Console.WriteLine 的参数,这样我就不需要按顺序传递 min、max 和 Grade?
作为相关说明,我经常删除要在 Console.WriteLine 中打印的变量,如下所示,其中 {2} 已被删除。
Console.WriteLine("{3} is not greater than {0} or smaller than {1}",min,max, grade);
Run Code Online (Sandbox Code Playgroud)
有没有办法不改变编号并说如下:
Console.WriteLine("{3} is not greater than {0} or smaller than {1}",{0}:min,{1}:max, {3}: grade);
Run Code Online (Sandbox Code Playgroud)
类似于这样的语句
@Html.ActionLink("Your profile", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" });
Run Code Online (Sandbox Code Playgroud)