1

私の課題では、プログラムのユーザーにいくつかの数値を入力するように依頼し、「5 で割り切れる範囲 [11, 131] の数値の量は...」などの特定の質問に回答する必要があります。この瞬間まで、私のプログラムは数字の量を提供しますが、具体的にはどの数字がその質問に対する答えであるかをユーザーに伝えたいです。私のコードは次のとおりです。

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変数には、11 から 131 までの 5 で割り切れる数の合計が含まれます。

于 2013-08-29T11:39:05.577 に答える