我的长度变量显示错误,我不知道该值

时间:2019-05-23 07:02:53

标签: c# visual-studio

using System;
using System.Collections.Generic;
using System.Linq;
namespace Dice {
    class Program {
        static void Main(string[] args) {
            int trialLength = 0;
            string trials = "";
            trialLength = Convert.ToInt32(Console.ReadLine());
            trials = Console.ReadLine();
            if (trialLength != trials.Length) throw new ArgumentOutOfRangeException("Length", "Trials Length is not matched");
            foreach(char ch in trials.ToCharArray()) {
                if (! (Convert.ToInt32(ch) >= 1) && !(Convert.ToInt32(ch) <= 6)) throw new ArgumentOutOfRangeException("Tials", "All 
Trials must be in range 1<=n<=6");
            }

            // exercise 1
            int sixs = 0;
            int twiceSixs = 0;
            int j = 0;
            bool f = true;
            for (int i = 0; i < trials.Length; i++) {
                if (f) j = (i - 1);
                char trial = trials[i];
                if (trial == '6' && j == (i - 1)) {
                    j = i;
                    sixs++;
                    f = false;
                } else {
                    if (sixs == 2) twiceSixs++;
                    sixs = 0;
                    f = true;
                }
            }
            if (sixs == 2) twiceSixs++;
            Console.WriteLine(twiceSixs);

            //exercise 2
            int tempSRLength = 0;
            int SuccessRLength = 0;
            j = 0;
            f = true;
            for (int i = 0; i < trials.Length; i++) {
                if (f) j = (i - 1);
                char trial = trials[i];
                if (inSuccessiveRange(trial) && j == (i - 1)) {
                    j = i;
                    tempSRLength++;
                    f = false;
                } else if (!f) {
                    if (tempSRLength > SuccessRLength) SuccessRLength = tempSRLength;
                    tempSRLength = 0;
                    f = true;
                }
            }
            Console.WriteLine(SuccessRLength);
            int LuckySeries = 0;
            j = 0;
            int k = 0;
            f = true;
            List < int > list = new List < int > ();
            for (int i = 0; i < trials.Length; i++) {
                if (f) j = (i - 1);
                char trial = trials[i];
                if ((trial == '6' || trial == '5') && j == (i - 1)) {
                    j = i;
                    LuckySeries++;
                    f = false;
                } else {
                    if (LuckySeries != 0) list.Add(LuckySeries);
                    LuckySeries = 0;
                    k++;
                    f = true;
                }
            }
            if (LuckySeries != 0)
            list.Add(LuckySeries);
            var most = list.GroupBy(i = >i).OrderByDescending(grp = >grp.Count()).Select(grp = >grp.Key).Max();
            Console.WriteLine(most);
            Console.Read();
        }

        public static bool inSuccessiveRange(char ch) {
            switch (ch) {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
                return true;
            default:
                return false;
            }
        }
    }
}

0 个答案:

没有答案