1

私は分割方法を使用して学生をスコアから分離しようとしています。次に、スコアが良いか悪いかプラスマイナス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

あなたのコードには多くの欠陥があります。私はそれらを修正しました。最初の最も明白なことは、配列をまったく使用してはならないということです。この名前とスコアの入力を繰り返したい場合でも、ユーザーは一度に 1 つの名前とスコアを入力しているため、配列は必要ありません。これは動作するように変更されたコードです:

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);
            }
        }
    }

ここで、ユーザーが終了を入力するまで多くの入力に対してこれを行いたいと思います。次に、 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 に答える