【问题标题】:How to establish a min max value, from a char array to same char array for output如何建立一个最小最大值,从一个字符数组到相同的字符数组进行输出
【发布时间】:2016-09-21 02:33:16
【问题描述】:

这是我的代码:

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

namespace ConsoleApplication7
{
    class Program
    {

        #region//Main() Put everything thats finished in here to output it to Console Apllication.
        static void Main(string[] args)
        {
            FirstPart();
        }
        #endregion



        #region
        static void FirstPart() // don't know what else to call this.
        {
            //Console.WriteLine(numS());// commented out

这就是我的问题所在。

            string pi = Console.ReadLine();
            PItoArray = pi.ToCharArray(pi.Min(),pi.Max());
            Console.WriteLine(PItoArray);

我的问题结束了。

        }
        #endregion

        #region //numS() gets number of char in player input.
        //static int numS()
        //{
        //    int num = PlayerInput().Length;
        //    return num;
        //}
        #endregion

        #region//PlayerInput() Gets whatever player writes in console apllication 
        static string PlayerInput()
        {  
            string S = Console.ReadLine();
            return S;
        }
        #endregion

        static char[] PItoArray;



    }
}

现在是这些细节(如果您需要更多,请随时回复。):

我尝试使用 num.max 而不是 pi.max,我个人偏好将所有内容拆分,但在这两种情况下,我都会遇到超出范围的异常,即索引为负数或大于集合 我认为它应该起作用的方式是它

  1. 将我写的任何内容设置为 pi 变量

  2. 然后将每个char拆分成char数组

  3. 然后使用 pi 来确定最小值和最大值。

  4. 那么它应该完全按照它的书写方式写出来。前任。 ""hello" 输入 变成 "hello" 输出。

现在这是我的 3 个问题:

  1. 我的逻辑正确吗?如果不是,请原谅我 18 岁,主要是我喜欢探索我能做什么和不能做什么。

  2. 如何使用 char 数组的最小值和最大值 ex。如果我写你好,我可以使用

    char[] Var = pi.ToCharArray(0,5);

    ConsoleWriteLine(Var);

输出会是“Hello”对吧?但是,如果我写了“Hello World”,我将如何获取字符串中的所有字符,无论所述字符串中的字符数量是多少,或者更好的询问方式是如何使用字符串中的字符数量来获取ToCharArray(min,max) 的最小值和最大值,所以如果我写了一个 10 个字母的句子或一个 100 个字母的句子,我永远不会得到超出范围的异常?

  1. 有没有办法在一个简单的 1-5 行代码中做到这一点?我并不懒惰,但更容易更容易,所以为什么不使用它。

【问题讨论】:

    标签: c# arrays string char minmax


    【解决方案1】:

    如果要将字符串转换为大小完全相同的 char 数组,您可以不带任何参数调用 pi.ToCharArray(),它会自动创建一个包含与原始字符串完全相同数量的字符的 char 数组。

    另外请注意,调用pi.Max() 将返回给您的是根据 ASCII 表具有最高字符代码的字符,而不是您所假设的字符串大小。 ("Hello, world".Max() 返回字符 'r',其代码为 114 - 字符串中最高的字符代码)。

    【讨论】:

      【解决方案2】:

      您的 Max 不能大于 pi.Length,因为这是字符串长度。 pi.ToCharArray 中的第一个参数 - Min - 是字符串的起始索引。您可以选择 Min 为 0 到 Max 之间的任何值。 pi.ToCharArray 中的第二个参数应该是您想要的 charArray 的长度,可以是从 0 到 Max - Min 的任何值。 您也可以阅读此文档:String.ToCharArray(int32 start, int32 length)

      【讨论】:

        【解决方案3】:

        再次您好,感谢您的快速回答,我的做法有点不同(尽管幸运的是你们两个人中的一个人说的完全正确。)这是我想出的:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        
        namespace SearchCharInString
        {
            class Program
            {
        
        
        
                #region//Main() Put everything thats finished in here to output it to Console Apllication.
                static void Main(string[] args)
                {
                    FirstPart();
                }
                #endregion
        
        
        
                #region // HERE IS WHERE CHANGED HAPPENED!!
                static void FirstPart()
                {
        
                    Console.WriteLine(PItoArray());
                }
                #endregion
        
        
        
                #region//PlayerInput() Gets whatever player writes in console apllication 
                static string PlayerInput()
                {
                    string S = Console.ReadLine();
        
                    return S;
                }
                #endregion
        

        这是我改变的

                #region // HERE IS WHERE CHANGED HAPPENED!!
                static char[]PItoArray() 
                    {
                    string PI = PlayerInput();
        
                    int piLength = PI.Length;
        
                    return PI.ToCharArray(0,piLength);
        
                    }
                #endregion
        
        
        
            }
        }
        

        我是这样称呼它的

        #region // HERE IS WHERE CHANGED HAPPENED!!
            static void FirstPart()
            {
        
                Console.WriteLine(PItoArray());
            }
            #endregion
        

        不过,我会认可您的答案,因为它有助于我进行研究。再次感谢各位。 我相信现在我应该能够在我的字符中搜索某些东西,甚至可以让它从 if 语句中使用字母索引到此代码中找出某些单词。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-09
          • 1970-01-01
          • 2022-11-21
          • 1970-01-01
          • 2021-12-31
          相关资源
          最近更新 更多