如何建立最小值,从char数组到同一个char数组,用于输出

时间:2016-09-21 02:33:17

标签: c# arrays string char minmax

这是我的代码:

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. 然后它应该完全按照它的编写方式写出来。恩。 ""你好"输入变成"你好"输出

  5. 现在这里有我提出的3个问题:

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

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

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

      ConsoleWriteLine(VAR);

    3. 输出将是" Hello"对?但如果我写了#34; Hello World"无论字符串中的字符串数量多少,我怎么能得到字符串中的所有字符串,或者更好的方式是如何使用字符串中的字符串数量?字符串得到一个min& ToCharArray的最大值(最小值,最大值)因此,如果我写了10个字母的句子或100个字母的句子,我永远不会超出范围异常?

      1. 有没有办法在简单的1-5行代码中执行此操作?我不是懒惰但更容易,所以为什么不使用它。

3 个答案:

答案 0 :(得分:0)

如果您希望将字符串转换为完全相同大小的char数组,则可以在不使用任何参数的情况下调用pi.ToCharArray(),并且它将自动创建一个char数组,其中包含原始字符数完全相同的字符数字符串。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

你好再次感谢快速回答我们做的有点不同(尽管我很幸运,其中有两个人说的确如此。)这是我想出的:

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语句中的某些单词。