列出作为我的作业答案的数字

时间:2013-08-29 11:37:06

标签: c#

在我的任务中,我必须要求我的程序的用户输入一些数字,然后给出具体问题的答案,例如:“范围内的数字数量[11,131]可被5整除......”等。直到这一刻,我的程序给出了数量,但我也希望它告诉用户,具体是哪个数字是该问题的答案。 我的代码如下:

int[] userInput = new int[10000];
        int counter = 0;
        int input10to75mod2 = 0;
        int nums_17_103_sq_div_9 = 0;
        int sum_17_103_sq_div9 = 0;
        int max = 0;
        int sum_of_sq = 0;
        int nums_div_of_largest = 0;
        int nums_div_of_sum_of_sq = 0;

        Console.WriteLine ("Please type some numbers");
        for (counter = 0; counter < userInput.Length; counter++) {
            string input = Console.ReadLine ();

            if (input == "" || input == "stop" || input == " ")
                break;
            else
                int.TryParse (input, out userInput [counter]);
        }

        int sum = 0;
        for (int i = 0; i < userInput.Length; i++) {
            sum += userInput [i];
        }

        for (int j = 0; j < userInput.Length; j++) {
            if (((userInput [j] % 2) == 0) && userInput [j] > 10 && userInput [j] < 75) {
                input10to75mod2++;
            }
            //ARITHMETIC MEAN OF NUMBERS IN RANGE [17,103] WHICH HAVE THEIR SQUARE DIVISIBLE BY 9
            if (userInput [j] >= 17 && userInput [j] <= 103 && ((userInput [j] * userInput [j]) % 9) == 0) {
                nums_17_103_sq_div_9++;
                sum_17_103_sq_div9 += userInput [j];
            }

            for (int r = 0; r < j; r++) {

                if (userInput [r] > max) {
                    max = userInput [r];
                }

                if ((userInput [r] % max) == 0 && max != userInput [r]) {
                    nums_div_of_largest++;
                }


            }


        }

        int mean = 0;
        int.Parse (mean.ToString ());

            if (nums_17_103_sq_div_9 > 0) {
                mean = sum_17_103_sq_div9 / nums_17_103_sq_div_9;
            }

        //CONCLUSIONS

        Console.WriteLine ("Conclusion 1: " + input10to75mod2); //Amount of numbers in range [10,75] that are divisible by 9
        Console.WriteLine ("Conclusion 2: " + sum); //Sum of all entered numbers
        Console.WriteLine ("Conclusion 3: " + nums_17_103_sq_div_9 + " AND " + sum_17_103_sq_div9); //Amount of numbers in range [17,103] with their square divisible by 9 and their sum
        Console.WriteLine ("Conclusion 4: " + mean); //Arithmetic mean of numbers in range [17,103] which have their square divisible by 9
        Console.WriteLine ("Conclusion 5: " + nums_div_of_largest); //Numbers that are divisors of largest of the entered numbers

        //Console.WriteLine ("Conclusion 6: " + nums_div_of_sum_of_sq); //Numbers that are divisors of sum of squares of entered numbers
        Console.ReadLine ();
        }

    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

var total = Enumerable.Range(11, 131).Count(x => x % 5 == 0);

total变量的总数在11到131之间,可被5整除。

答案 1 :(得分:0)

您可以将用户输入存储在list

List<int> acceptedAnswers = new List<int>();

if (userInput [j] >= 17 && userInput [j] <= 103 && ((userInput [j] * userInput [j]) % 9) == 0) {
   acceptedAnswers.Add(userInput[j]);
}

然后您可以迭代acceptedAnswers并显示输出。

foreach (var answer in acceptedAnswers) {
    Console.WriteLine(answer.ToString());
}