1

我正在尝试使用 split 方法将学生与分数分开,然后如果分数是好或坏加上你的负 100,而不是输出一条消息。split 方法无法将 char 转换为字符串

class Program
    {   //Here we declare some Const variables 
        const int MAX = 1;
        const int ZERO = 0;
        const int ONE = 1;

        static void Main(string[] args)
        {   //here we declare the variables and the 2 arrays for the main method

            int perfecto = 100;
            string input;
            string[] student = new string[MAX];
            int[] score = new int[MAX];            


            //this will be the introduction for the program nice and friendly.
            Console.WriteLine("Welcome to the Test score calculator!!");



                    Console.Write("\nPlease Input your name and your score, seperated by a space: ");
                    input = Console.ReadLine();



                    Console.WriteLine("Welcome to the Test score calculator!!");
                    Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter: ");
                    input = Console.ReadLine();




                    //SPLIT METHOD ACTIVATED.. here we call the split method 
                    SplitMethod(input, ref student, ref score);




            //Here we call the output           
            Output(student, score, perfecto);

            Console.ReadLine();
        }
        //Here is the split method. this will take the two kinds of data and split them into 2 arrays
        //the string and the int seperate so that its easyer to make calculations. 
        //also we referenced these arrays 
        static void SplitMethod(string input, ref string[] student, ref int[] score)
        {
            char rules = { ' ', '\r' };
            string splitArray = input.Split();

            //here is the actual split
            student = splitArray[ZERO];
            score = int.Parse(splitArray[]);
            return;
        }     

        static void Output(string[] student, int[] score, int perfecto)
        {         


                //here is the added if statement for the perfect score scenario 
                if (score[i] > perfecto)
                {
                    //here is the output incase someone scores a perfect game
                    Console.WriteLine("\n{0}'s score was {1}*...Congrats {2} you qualify for the TEAM!!!", student[], score[], student[]);
                }
                else
                {
                    //and if they dont it displayes here.
                    Console.WriteLine("\nSorry {0}, {1} is not good enough. \nIm afraid you dont qualify, but keep it up!", student[], score[]);
                }
            }

        }
    }
4

3 回答 3

1

首先,您应该将字符数组声明为数组

        char[] rules = { ' ', '\r' };

其次,您应该将规则传递给您的拆分

        string[] splitArray = input.Split(rules);

我相信您还有其他编译器错误。你应该尝试自己做一些

当您提出问题时,您还应该告诉我们您的编译错误是什么。

于 2012-11-13T19:58:08.077 回答
1

阅读有关String.Split的文档

string[] splitArray = input.Split(rules);

您还需要修复规则的定义,使其成为 char 数组。再次,查看文档

于 2012-11-13T19:58:32.737 回答
0

你的代码有很多缺陷,我纠正了它们。第一个也是最明显的一点是您根本不应该使用数组。即使您想重复输入此名称和分数,也不需要数组,因为用户一次输入一个名称和分数。这是您的代码更改为工作:

class Program
    {   

        static void Main(string[] args)
        {   //here we declare the variables and the 2 arrays for the main method

            int perfecto = 100;
            string input;
            string student = string.Empty;
            int score = 0;            


            //this will be the introduction for the program nice and friendly.
            Console.WriteLine("Welcome to the Test score calculator!!");
            Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter: ");
            input = Console.ReadLine();

            //SPLIT METHOD ACTIVATED.. here we call the split method 
            SplitMethod(input, ref student, ref score);

            //Here we call the output           
            Output(student, score, perfecto);

            Console.ReadLine();
        }
        //Here is the split method. this will take the two kinds of data and split them into 2 arrays
        //the string and the int seperate so that its easyer to make calculations. 
        //also we referenced these arrays 
        static void SplitMethod(string input, ref string student, ref int score)
        {
            char [] rules = { ' ', '\r' };
            string [] splitArray = input.Split(rules);

            //here is the actual split
            if(splitArray.Length>1)
            {
                student = splitArray[0];
                int.TryParse(splitArray[1], out score);
            }
        }     

        static void Output(string student, int score, int perfecto)
        {         
            //here is the added if statement for the perfect score scenario 
            if (score > perfecto)
            {
                //here is the output incase someone scores a perfect game
                Console.WriteLine("\n{0}'s score was {1}*...Congrats {2} you qualify for the TEAM!!!", student, score, student);
            }
            else
            {
                //and if they dont it displayes here.
                Console.WriteLine("\nSorry {0}, {1} is not good enough. \nIm afraid you dont qualify, but keep it up!", student, score);
            }
        }
    }

现在我假设您想对许多输入执行此操作,直到用户输入 end,然后您应该像这样更改 Main 方法:

static void Main(string[] args)
        {   //here we declare the variables and the 2 arrays for the main method

            int perfecto = 100;
            string input;
            string student = string.Empty;
            int score = 0;            


            //this will be the introduction for the program nice and friendly.
            Console.WriteLine("Welcome to the Test score calculator!!");
            Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter, insert END if you want to finish: ");

            while (!(input = Console.ReadLine()).Equals("end", StringComparison.CurrentCultureIgnoreCase))
            {
                SplitMethod(input, ref student, ref score);
                Output(student, score, perfecto);
                Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter, insert END if you want to finish: ");
            }
            Console.ReadLine();
        }
于 2012-11-13T20:17:42.913 回答