C# 如何将&引用;当另一个数字出现时,但不是当有更多的数字显示时#

C# 如何将&引用;当另一个数字出现时,但不是当有更多的数字显示时#,c#,primes,C#,Primes,我试图在C#上制作一个简单的程序,每次它找到一个素数时,它都会在它们之间显示一个,,但当其后不再有素数时就不会了。它从一个文件中读取,该文件将显示从1到.txt文件中给定的数字X的所有素数 static void Main(string[] args) { using (StreamReader reader = File.OpenText(@"(path to txt")) while (!reader.EndOfStream)

我试图在C#上制作一个简单的程序,每次它找到一个素数时,它都会在它们之间显示一个
,但当其后不再有素数时就不会了。它从一个文件中读取,该文件将显示从1到.txt文件中给定的数字
X
的所有素数

    static void Main(string[] args)
    {
        using (StreamReader reader = File.OpenText(@"(path to txt"))
            while (!reader.EndOfStream)
            {
                int line = Convert.ToInt32(reader.ReadLine());
                int testsDone = 1;
                if (line == 1) Console.WriteLine("1");
                else
                {
                    Console.Write("1,");
                    while (testsDone <= line)
                    {
                        if (PrimeCheck(num) == true)
                        {
                            Console.Write("{0}", num);
                            if (testsDone < line) { Console.Write(","); }
                        }
                        //else Console.Write("( {0} ),", num); <-- testing
                        testsDone++;
                        num++;

                    }
                    Console.WriteLine("");
                    num = onum;
                }
            }
    }

    private static bool PrimeCheck(int num)
    {
        if (num == 2 || num == 3 || num == 5) return true;
        else if (num % 2 != 0 && num % 3 != 0 && num % 5 != 0)
        {
            for (int div = 3; div < num; div += 2)
            {
                if (num % div == 0) return false;
            }
            return true;
        }
        else if (num == 1) return false;
        else return false;
    }
我得到的输出:

2,3,5,7

2,3,

我试图获得的输出:

2,3,5,7

2,3


只有当它确定有额外的值要输出时,才让它添加逗号,而不是仅仅因为没有达到输入值就添加逗号

Console.Write("1");
while (testsDone <= line)
{
    if (PrimeCheck(num) == true)
    {
        Console.Write(", {0}", num);
    }
    testsDone++;
    num++;               
}
Console.Write(“1”);

while(testsDone从
1
的第一次写入中删除
,然后将其放在之后输出的每个数字之前:

Console.Write("1");
while (testsDone <= line) {
    if (PrimeCheck(num) == true) {        
        if (testsDone < line) { Console.Write(","); }
        Console.Write("{0}", num);
    }
    testsDone++;
    num++;
}
Console.Write(“1”);

while(testsDone您应该能够将,放在每个素数的输出之前,例如

Console.Write("1");

while (testsDone <= line)
{
    if (PrimeCheck(num) == true)
    {
       Console.Write(", {0}", num);
    }

    testsDone++;
    num++;
}
Console.Write(“1”);

而(testsDone以防有人想找到办法


如果您将该数字字符串转换为它自己的字符串变量,那么可以执行以下操作

string primeNumbers = "";
//add numbers to string
primeNumbers = primeNumbers.Substring(0, primeNumbers.Length - 1);

希望这有帮助。

以下是该问题的通用解决方案,使用伪代码:

separator <- ", "

firstItem <- true;

for each item in itemList
  if (firstItem)
    firstItem <- false
  else
    print separator
  endif

  print item
end for

分隔符不带逗号(1)开始,然后当您找到一个主写控制台时。write(“,{0}”,num);“这是非常简单的使用
字符串。Join(“,”,someList)
函数创建一个列表来保存值,否则显示单个值…非常简单我只是意识到我的编辑与David相同。甚至没有看到在我模糊的答案之后还有其他答案。
string primeNumbers = "";
//add numbers to string
primeNumbers = primeNumbers.Substring(0, primeNumbers.Length - 1);
separator <- ", "

firstItem <- true;

for each item in itemList
  if (firstItem)
    firstItem <- false
  else
    print separator
  endif

  print item
end for