C# 如何在变量c中打印数据#
我试图打印“count”,因为它保存了我的结果,但我似乎无法管理。基本上,我需要打印这组代码的结果。有什么建议吗?控制台写入线(“计数:”,计数);告诉我count在当前上下文中不存在。我尝试将count方法更改为public int,但仍然相同。我是否尝试在错误的位置打印,还是需要创建一个变量来声明countC# 如何在变量c中打印数据#,c#,C#,我试图打印“count”,因为它保存了我的结果,但我似乎无法管理。基本上,我需要打印这组代码的结果。有什么建议吗?控制台写入线(“计数:”,计数);告诉我count在当前上下文中不存在。我尝试将count方法更改为public int,但仍然相同。我是否尝试在错误的位置打印,还是需要创建一个变量来声明count private int Test(Char[,] data, int x, int y) { try { if(data[x, y] == '*')
private int Test(Char[,] data, int x, int y)
{
try
{
if(data[x, y] == '*')
return 1;
else
return 0;
}
catch
{
return 0;
}
}
private int GetCount(Char[,] data, int x, int y)
{
int count = 0;
count += Test(data, -1, -1);
count += Test(data, 1, 1);
count += Test(data, -1, 1);
count += Test(data, 1, -1);
count += Test(data, 0, -1);
count += Test(data, 0, 1);
count += Test(data, 1, 0);
count += Test(data, -1, 0);
return count;
}
try
{
var rows = int.Parse(inputSize[0]);
var cols = int.Parse(inputSize[1]);
Char[,] Template = new Char[rows, cols];
foreach( var rowId in Enumerable.Range(0, rows))
{
var inputValue = Console.ReadLine();
if (inputValue.Length == cols)
{
if (inputValue.All(x => x == '*' || x == '.'))
{
// convert string to array.
char[] array1 = inputValue.ToCharArray();
// Loop through array.
for (int i = 0; i < array1.Length; i++)
{
char inputChar = array1[i];
// Get character from array.
Template[rowId, i] = inputChar;
}
}
else
{
Console.WriteLine($"Input value '{inputValue}' has unaccaptable characters");
}
}
else
{
Console.WriteLine($"Value length of '{inputValue}' does not match of column size {cols}");
}
}
Console.WriteLine("Finished");
Console.WriteLine("Count: ", count); //This line is not working
}
private int Test(字符[,]数据,int x,int y)
{
尝试
{
如果(数据[x,y]='*')
返回1;
其他的
返回0;
}
抓住
{
返回0;
}
}
私有整数GetCount(字符[,]数据,整数x,整数y)
{
整数计数=0;
计数+=测试(数据,-1,-1);
计数+=测试(数据,1,1);
计数+=测试(数据,-1,1);
计数+=测试(数据,1,-1);
计数+=测试(数据,0,-1);
计数+=测试(数据,0,1);
计数+=测试(数据,1,0);
计数+=测试(数据,-1,0);
返回计数;
}
尝试
{
var rows=int.Parse(inputSize[0]);
var cols=int.Parse(inputSize[1]);
字符[,]模板=新字符[行,列];
foreach(可枚举范围(0,行)中的var rowId)
{
var inputValue=Console.ReadLine();
if(inputValue.Length==cols)
{
if(inputValue.All(x=>x='*'| | x='.'))
{
//将字符串转换为数组。
char[]array1=inputValue.ToCharArray();
//循环遍历数组。
for(int i=0;i
如果您正在使用控制台,只需编写
Console.WriteLine("Count: {0}", count);
Console.WriteLine(Convert.ToString(count));//这也可能有效,但不确定。
希望这就是你想要的答案 一切都是一个
对象
,对象有一个名为ToString()
的函数您可以根据需要对字符串进行压缩:
Console.WriteLine(count.ToString()); // works with any object, some may give nonsense though.
Console.WriteLine("The value of count is " + count.ToString()); // concatination
Console.WriteLine(count); // WriteLine has an overload for integer
Console.WriteLine(count.ToString("N2")); // up to two decimal places, but will use the local culture.
Console.WriteLine(count.ToString("N2", System.Globalization.CultureInfo.InvariantCulture)); // The same, but with invariant culture
Console.WriteLine("Count is {0}", count); // format string
Console.WriteLine(String.Format("Count is {0}", count)); // built string from format string before passing to console.writeline
可能有更多的方式向控制台显示整数
我想您遗漏了一些代码?
Test
方法做什么?Console.WriteLine(count)代码>我已将测试方法添加到问题中,请阅读页面。然后,问题将显示一个显示问题的最小示例。(提示:Main
方法在哪里?)添加了更多的代码Console.WriteLine()
方法有一个接受int的重载,因此您不需要第二个示例中的Convert.ToString
方法-只需Console.WriteLine(count)代码>啊,谢谢你的更正,看,我不太确定它是否只能接受字符串。你可以在线查找:我已经尝试过这些,但是如果我使用方法,它会告诉我无法访问的代码。如果我在方法之外执行此操作,它会告诉我当前上下文中不存在名称“count”。您可以做的是,因为您返回的是count,您可以在Console.WriteLine()内调用函数,如:Console.WriteLine(GetCount(x,y,z,a)
不能肯定这会起作用,但似乎值得一试。这些都很好,但我的问题是它没有在GetCount方法中声明,我需要声明它,但我不确定如何声明,因为我需要它与GetCount方法的结果相等。@JulianZahra我不理解你的问题。你没有调用你的Get任何地方都可以使用计数方法。