Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将查找素数的算法的结果存储在文件(C#)中_C#_Algorithm_Primes - Fatal编程技术网

将查找素数的算法的结果存储在文件(C#)中

将查找素数的算法的结果存储在文件(C#)中,c#,algorithm,primes,C#,Algorithm,Primes,我正在尝试制作一个程序,查找素数,在控制台中显示它们,并将这些数字存储在一个文件中。程序已经将数字存储在一个文件中,但它不会在控制台中显示数字。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Priemgetallen { class

我正在尝试制作一个程序,查找素数,在控制台中显示它们,并将这些数字存储在一个文件中。程序已经将数字存储在一个文件中,但它不会在控制台中显示数字。这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Priemgetallen
{
    class Program
    {
    static void Main()
    {
        using (StreamWriter writer = new StreamWriter("C://Users//mens//Documents//PriemGetallen.txt"))
        {
            Console.SetOut(writer);
            Act();
        }
    }

    static void Act()
    {

        double maxGetal = double.MaxValue;
        Console.WriteLine("--- Primes between 0 and 100 ---");
        for (int i = 0; i < maxGetal; i++)
        {
            bool prime = PrimeTool.IsPrime(i);
            if (prime)
            {
                Console.Write("Prime: ");
                Console.WriteLine(i);
            }
        }
    }

    public static class PrimeTool
    {
        public static bool IsPrime(int candidate)
        {
            // Test whether the parameter is a prime number.
            if ((candidate & 1) == 0)
            {
                if (candidate == 2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            // Note:
            // ... This version was changed to test the square.
            // ... Original version tested against the square root.
            // ... Also we exclude 1 at the end.
            for (int i = 3; (i * i) <= candidate; i += 2)
            {
                if ((candidate % i) == 0)
                {
                    return false;
                }
            }
            return candidate != 1;
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
名称空间Priemgetallen
{
班级计划
{
静态void Main()
{
使用(StreamWriter writer=newstreamwriter(“C://Users//mens//Documents//PriemGetallen.txt”))
{
控制台。放样(编写器);
Act();
}
}
静态无效行为
{
double maxGetal=double.MaxValue;
Console.WriteLine(“--Primes介于0和100之间--”);
对于(int i=0;i对于(inti=3;(i*i),这是因为行
Console.SetOut(writer);
。您正在向文件发送控制台输出

与其按您现在的方式操作,不如放弃
StreamWriter
,改用:

if (prime)
{
    var primeText = string.Format("Prime: {0}", i);
    Console.WriteLine(primeText );
    File.AppendAllText(@"C:\Users\mens\Documents\PriemGetallen.txt",
                       primeText + Environment.NewLine);
}

哪一行不起作用?标题表明您不知道如何将它们存储在文件中,但随后您声明程序已经存储了这些数字。那么问题是什么?您不应该将控制台更改为新的流,同时写入控制台和单独的文件,而不是依赖console.out来同时执行这两项操作。它不会输出编译器如果出现错误或异常,它只是不显示数字。我认为这与我试图显示另一个方法中的int有关,但我可能错了。我仍在学习,您可能希望检查用于计算给定最大值的所有素数的。不,这是因为您将输出重定向到一个文件,而不是onger有一个控制台可供写入。(或者至少不再连接到它)@ThymenVanScheppingen,请查看更新的答案。@ThymenVanScheppingen,糟糕透了。我甚至输入了
if(prime)…
并且没有记录它已经被使用的事实。答案再次更新,为文本提供了一个新的变量名!它显示了一个异常,但是当我将“Prime:{I}”改为“Prime:{0}”时,它工作了。非常感谢:)祝您有愉快的一天。