无法将我的方法设置为大写我的字符串数组

时间:2016-04-03 05:34:42

标签: c# loops

以后列出了整个程序,但我遇到的唯一问题是ToUppers()方法。我只是希望这个方法迭代我的数组中的每个字符串,然后将所有内容都设置为大写。

        private static string[] ToUppers(string[] stringToUpperArrays)
        {
            string stringer;

            foreach (string value in stringToUpperArrays)
            {
                stringer = value.ToUpper(); // <== this line highlighted
                Console.WriteLine(stringer);
            }
            return stringToUpperArrays;
        }

程序在控制台上打印后执行,并列出NullReferenceException,并突出显示stringer = value.ToUpper();

整个计划

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Mark Bouwman
            //CNT A01
            //ICA18
            //April 2nd

            string answer;
            int numInArray;
            string[] stringArrays;
            string[] stringArraysToDisplay;
            string[] stringToUpperArrays;

            //TITLE
            Console.WriteLine("\t\tStringy");

            do
            {
                numInArray = Utility.Utility.GetInt(2, 10, "Enter the size of the array from 2 to 10: ");
                stringArrays = CreateArray(numInArray);
                stringArraysToDisplay = Display(stringArrays);
                stringToUpperArrays = ToUppers(stringArrays);






                //aksing to run program again
                Console.Write("Run program again? yes or no: ");
                answer = Console.ReadLine();                
            }
            while (answer.Equals("yes", StringComparison.CurrentCultureIgnoreCase));
        }

        private static string[] CreateArray(int numInArray)
        {
            int index;
            string[] stringArray;

            stringArray = new string[(numInArray + 1)];
            for (index = 0; index < numInArray; ++index)
            {
                Console.Write("Enter string #" + (index + 1) + " ");
                stringArray[index] = Console.ReadLine();
            }
            return stringArray;
        }

        private static string[] Display(string[] stringArraysDisplay)
        {
            foreach (string value in stringArraysDisplay)
            {
                Console.WriteLine(value);
            }
            return stringArraysDisplay;
        }

        private static string[] ToUppers(string[] stringToUpperArrays)
        {
            string stringer;

            foreach (string value in stringToUpperArrays)
            {
                stringer = value.ToUpper();
                Console.WriteLine(stringer);
            }
            return stringToUpperArrays;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

CreateArray中,您将数组定义为numInArray + 1的大小,但只使用numInArray字符串填充数组,即最后一个索引为空。当您在空索引上尝试value.ToUpper();时,您将获得异常。

CreateArray更改

stringArray = new string[(numInArray + 1)];

stringArray = new string[numInArray];

或更改

for (index = 0; index < numInArray; ++index)
{
    Console.Write("Enter string #" + (index + 1) + " ");
    stringArray[index] = Console.ReadLine();
}

for (index = 0; index < stringArray.Length; ++index)
{
    Console.Write("Enter string #" + (index + 1) + " ");
    stringArray[index] = Console.ReadLine();
}

答案 1 :(得分:-1)

初始化数组时应该擦除+ 1

stringArray = new string[(numInArray)];

在这个方法中

 private static string[] CreateArray(int numInArray)