C# C语言中的溢出问题#
我的错误是: cs1501方法“Write”的无重载包含2个参数 这是程序本身。错误发生在写入程序上C# C语言中的溢出问题#,c#,C#,我的错误是: cs1501方法“Write”的无重载包含2个参数 这是程序本身。错误发生在写入程序上 using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { const string
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
const string fileName = "Primes12345678910.txt";
static void Main(string[] args)
{
int c = 1;
int a = 1;
int b = 1;
int olda = 1;
int oldb = 1;
while (true)
{
if (a * b == 1)
{
a = a + 1;
}
if (a * b == c)
{
Console.WriteLine("{0} is not prime.", c);
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write("{0}", c);
}
}
}
}
}
}
该函数只接受一个参数,因此remove{0}
,它应该是这样的:
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(c);
// Unless you want 'c' to be a string, then use
writer.Write(c.ToString());
// You can also use StringFormat
writer.Write(string.Format("{0}", c));
}
另一个选项是@ScottChamberlain建议的,使用a,因为它支持您正在尝试的操作:
仔细查看编译错误:
No overload for method 'Write' takes 2 arguments
在本例中,它的意思正好是:Write
方法不接受两个参数
正如其他人所指出的,问题在于:
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write("{0}", c);
}
你要么想做
writer.Write(c.ToString());
正如@BogDoeJoe所建议的,或者,如果您打算使用格式字符串,请执行以下操作
writer.Write(string.Format("{0}", c));
在这种情况下,格式字符串是毫无意义的
另外,作为一个简短的提示,我不确定这段代码是否不完整,但它现在实际上做不了什么。它实际上要做的就是在循环的第一次迭代中将
a
设置为2;在那之后,它实际上什么也不做,因为a*b=2
和c=1
(因此,在第一次迭代之后,您的if
语句都不可能再次为真,并且您将永远不会实际向控制台写入任何内容)。使用writer.write(string.Format({0},c));没有采用字符串和对象的BinaryWriter.Write
方法。您可能正在寻找StreamWriter
,它确实有这样一种方法。您能澄清一下您在这里试图实现什么吗?我不确定这段代码是否不完整,但即使它确实编译了,也不会有太多作用。这将有非常不同的行为,这取决于c
的类型,因为如果它是1
的int
,它将写入0x00000001
。如果您想要相同的行为,则必须执行writer.Write(c.ToString())
操作,该操作将写入0x000139
,因为它使用两个字节写入长度前缀,然后写入字符串1
的UTF-16值,即0x39
。然而,看看代码,我认为op只是使用了一个StreamWriter
,因为他们只是试图将素数列表写入一个文件,而且该文件也是在创建模式下打开的,他和你们都在每个循环中覆盖它。@ScottChamberlain c是一个int。BinaryWriter支持整数参数。谢谢你的评论,我已经在答案中添加了StreamWriter
。谢谢你,是的,它还没有完成。这只是我在几分钟内完成的。
writer.Write(string.Format("{0}", c));