本文介绍了无法在我的代码中找到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有人可以查看下面的代码并提出问题区域。Can someone please check the code below and mention the problem area.<pre>static void Main(string[] args) { Console.Write("How many nums to be compared: "); int n = int.Parse(Console.ReadLine()); //Parse int n for arr.Length int[] arr = new int[n]; //Creates array with name arr and length n Console.WriteLine("Enter the nums: "); //To be used while entering elements //Registers array elements for (int arrIndex = 0; arrIndex < arr.Length; arrIndex++) { arr[arrIndex] = int.Parse(Console.ReadLine()); //Parse array elements } //Prints array for (int arrIndex = 0; arrIndex < arr.Length; arrIndex++) { Console.Write(arr[arrIndex] + " "); } Console.WriteLine(); GetMax(arr); } static void GetMax(int[] array) { if ((array[0]) > (array[1])) { Console.Write("Number {} is > than {}", array[0], array[1]); } else if ((array[0]) < (array[1])) { Console.Write("Number {} is < than {}", array[0], array[1]); } else { Console.Write("Number {} and {} are equal", array[0], array[1]); } }} 我尝试了什么: 我试着从我正在学习C#的书中重新阅读有关方法的完整章节,但找不到任何理由。 我是一个绝对的初学者,所以我请求保持解释非常简单。 当我运行代码时,它给我以下错误What I have tried:I tried re-reading the complete chapter on methods from the book I'm learning C# but couldn't find any reason.I'm an absolute beginner so I request to please keep the explanation very very simple.It gives me the following error when I run the codeHow many nums to be compared: 2Enter the nums:151 5Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.IO.TextWriter.Write(String format, Object arg0, Object arg1) at System.IO.TextWriter.SyncTextWriter.Write(String format, Object arg0, Object arg1) at System.Console.Write(String format, Object arg0, Object arg1) at Ch9ExQ2.Program.GetMax(Int32[] array) in C:\Users\mohit\source\ProgFundamExrsQs\Ch9ExQ2\Ch9ExQ2\Program.cs:line 42 at Ch9ExQ2.Program.Main(String[] args) in C:\Users\mohit\source\ProgFundamExrsQs\Ch9ExQ2\Ch9ExQ2\Program.cs:line 31Press any key to continue . . .推荐答案您的问题出在 Console.Write 函数中 - 您忘记了将变量的索引放在输出中。你还应该在 Main 方法的末尾添加一个 Console.ReadKey(); ,因为你会这样,否则,从Visual Studio启动程序时看不到输出...Your problem is in the Console.Write function - you forgot to put the index for the variable to be shown in the output. You should also add a Console.ReadKey(); call at the end of the Main method because you will, otherwise, not see the output when you start the program from Visual Studio...static void GetMax(int[] array) { if ((array[0]) > (array[1])) { Console.Write("Number {0} is > than {1}", array[0], array[1]); } else if ((array[0]) < (array[1])) { Console.Write("Number {0} is < than {1}", array[0], array[1]); } else { Console.Write("Number {0} and {1} are equal", array[0], array[1]); } }感谢你指出这一点。 下次我将在重新检查我的代码时更加专注。 再次感谢。是的,该计划现在正在运作。Thanks for pointing that out.Next time i will be more attentive while rechecking my codes.Thanks again. Yes the program is working now. 这篇关于无法在我的代码中找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 12:14