1

在我的作业中,我必须要求我的程序的用户输入一些数字,然后回答具体问题,例如:“范围 [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 ();
        }

    }
}
4

2 回答 2

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());
}
于 2013-08-29T11:41:28.563 回答
0

你可以尝试这样的事情:

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

total变量将具有可被 5 整除的 11 到 131 之间的数字总数。

于 2013-08-29T11:39:05.577 回答