C# 如何修复赢得';同一个输入不能工作两次
我试图制作一个简单的程序,要求输入一个整数,如果这个整数是素数,则输出它的因子。如果用户的输入不是基本因子,它会要求用户输入基本因子。问题是,如果我在一行中输入两次有效的素数因子,它就不会给出正确的输出。我已经在这方面工作了一段时间了,我想这肯定是我所缺少的东西,因为我无法理解它C# 如何修复赢得';同一个输入不能工作两次,c#,math,C#,Math,我试图制作一个简单的程序,要求输入一个整数,如果这个整数是素数,则输出它的因子。如果用户的输入不是基本因子,它会要求用户输入基本因子。问题是,如果我在一行中输入两次有效的素数因子,它就不会给出正确的输出。我已经在这方面工作了一段时间了,我想这肯定是我所缺少的东西,因为我无法理解它 class Program { static void Main(string[] args) { List<int> factors = new List<int&g
class Program
{
static void Main(string[] args)
{
List<int> factors = new List<int>();
int a, b, c;
Console.Write("Hello,\n Please enter an integer: ");
string userInput = Console.ReadLine();
while (userInput != "quit")
{
try
{
a = int.Parse(userInput);
c = a;
bool negative = a < 0;
int letter = Convert.ToInt32(Int32.TryParse(userInput, out letter));
if (!negative && letter != 0)
{
for (b = 2; a > 1;)
if (a % b == 0)
{
while (c % b == 0)
{
c /= b;
factors.Add(c);
}
Console.WriteLine($"{a} has factors: { String.Join(", ", factors)}");
Console.Write("Please enter another integer: ");
factors.Clear();
userInput = Console.ReadLine();
a = int.Parse(userInput);
}
}
else
{
Console.Write("Please enter a valid prime factor: ");
userInput = Console.ReadLine();
}
}
catch
{
Console.Write("Please enter a valid prime factor: ");
userInput = Console.ReadLine();
}
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
列表因子=新列表();
INTA、b、c;
控制台。写入(“您好,\n请输入一个整数:”;
字符串userInput=Console.ReadLine();
while(userInput!=“退出”)
{
尝试
{
a=int.Parse(用户输入);
c=a;
布尔负=a<0;
int letter=Convert.ToInt32(Int32.TryParse(userInput,out letter));
如果(!否定和字母!=0)
{
对于(b=2;a>1;)
如果(a%b==0)
{
而(c%b==0)
{
c/=b;
增加(c);
}
WriteLine($“{a}有因子:{String.Join(“,”,factors)}”);
控制台。写入(“请输入另一个整数:”);
因子;
userInput=Console.ReadLine();
a=int.Parse(用户输入);
}
}
其他的
{
Console.Write(“请输入有效的素数:”);
userInput=Console.ReadLine();
}
}
抓住
{
Console.Write(“请输入有效的素数:”);
userInput=Console.ReadLine();
}
}
}
}
你好,,
请输入一个整数:64…64具有因子:32、16、8、4、2、1 请输入另一个整数:y..
请输入有效的基本因子: 现在,如果我连续做两个有效的素数因子 你好,, 请输入一个整数:64…
64具有因子:32、16、8、4、2、1 请输入另一个整数:8…
8有以下因素: 请输入另一个整数: 然而 你好,, 请输入一个整数:64…
64具有因子:32、16、8、4、2、1 请输入另一个整数:y..
请输入有效的基本因子:8 8具有以下因素:4、2、1
请输入另一个整数:通常我不会只提供答案,因为开发人员能够自己解决这些问题很重要。但我无法在评论中作出回应。因此,我将提供一些引导我找到解决方案的反馈和一些一般性意见 如果您花时间调试,您将能够看到代码中的问题。添加简单的Console.WriteLines以指示代码流中哪里开始出错 您还需要为变量提供更好的名称。a、 b,c是没有意义的,很难理解的目的。更好的变量名意味着我们可以更好地理解您遇到的问题以及代码试图做什么。一个写得好的代码块应该在5秒内可读,并且通常可以理解。我尽了最大的努力将它应用到您的代码中,而不进行太多的重构,这样您就可以在比较中看到代码中的冗余点 问题是,当我调试代码时,您的代码似乎出现了无限循环,或者int解析出现了一些奇怪的现象。因为我不想花太多的时间去理解为什么会发生这种情况,所以我对代码提出了上述建议,看看是否仅仅通过降低代码复杂性和提高可读性就可以降低错误发生的风险 下面的解决方案可能不是100%符合你所追求的,很难知道基于缺乏预期的结果
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
List<int> factors = new List<int>();
while (true)
{
Console.WriteLine("Please enter an integer: ");
var userInput = Console.ReadLine();
if (userInput.Equals("quit"))
break;
if (!Int32.TryParse(userInput, out var firstOut)
|| !Int32.TryParse(userInput, out var secondOut))
continue;
if (firstOut % 2 != 0 || firstOut < 0)
continue;
while (secondOut % 2 == 0)
{
secondOut /= 2;
factors.Add(secondOut);
}
Console.WriteLine($"{firstOut} has factors: {String.Join(", ", factors)}");
factors.Clear();
}
}
}
使用系统;
使用System.Collections.Generic;
公共课程
{
公共静态void Main(字符串[]args)
{
列表因子=新列表();
while(true)
{
Console.WriteLine(“请输入一个整数:”);
var userInput=Console.ReadLine();
if(userInput.Equals(“退出”))
打破
if(!Int32.TryParse(userInput,out var firstOut)
||!Int32.TryParse(userInput,out var secondOut))
继续;
if(firstOut%2!=0 | | firstOut<0)
继续;
而(第二输出%2==0)
{
secondOut/=2;
因素。添加(第二次输出);
}
WriteLine($“{firstOut}有因子:{String.Join(“,”,factors)}”);
因子;
}
}
}
请重构您的代码,这样您就可以在没有用户输入的情况下显示问题,然后在代码内联显示所有数据的情况下使用true进行发布。旁注:使用好的长变量名要比“a”、“b”、“c”好得多(仅略好于x342、x564、x234)正如您所知,该值在代码中是如何使用的,以及在特定代码段中进行更改是否有意义。for循环没有块。您还需要共享示例输入和预期输出。你调试代码了吗?@AlexeiLevenkov我添加了输入和输出的示例,显示了哪些有效,哪些无效。我认为我不能再减少代码了。谢谢你对我的建议naming@ChetanRanpariya我在bottomThank@lachlan.p.jordan上添加了输入/输出示例。这修复了我试图解决的一个问题,但是,使我回到了处理