数组乘法对

时间:2019-01-04 02:27:15

标签: c# arrays c#-4.0

我必须在C#中创建代码以实现:

  • 要求用户在数组中输入任意数字集并显示所有输入的数字。 我完成了这一部分,代码正常运行

  • 然后将数字对相乘并显示结果。如果您有奇数个数字,则只需显示最后一个数字。

    E.G。 2 3 8 4变成6 32 2 3 8 4 7变成6 32 7

我的问题是奇数数组。如果数组的元素个数为偶数,这没问题,但是如果数组的元素个数为奇数,则有错误,请不要打印最后一个元素。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayDisplay
{
    class Array
    {
        static void Main(string[] args)
        {
            int[] array = new int[9];
            int userInput = -1;
            int itt = 0;
            int count = 0;

            Console.WriteLine("Enter a integer number or -1 to exit:");
            userInput = Convert.ToInt32(Console.ReadLine());
            count++;
            while (userInput != -1 && itt<array.Length)
            {

                array[itt] = userInput;
                itt++;

                Console.WriteLine("Enter a integer number or -1 to exit:");
                userInput = Convert.ToInt32(Console.ReadLine());
                count++;
            }
              Console.WriteLine("The array contains: ");

            for (int i = 0; i < array.Length; i++) {
                Console.Write(" {0} , "  ,array[i]);

            }
            Console.WriteLine("");
            Console.WriteLine("count {0}",count-1);

            if (count % 2 == 0)
            {
                for (int i = 0; i < array.Length; i += 2)
                {
                    Console.Write(" {0} , ", array[i] * array[i + 1]);
                }
            }
            else {
                for (int i = 0; i < array.Length; i += 2)
                {
                    Console.Write(" {0} , ", array[i] * array[i + 1])
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

由于7 * 0 = 0,请尝试以下操作:

        if (count % 2 == 0)
        {
            for (int i = 0; i < array.Length - 1; i += 2)
            {
                Console.Write(" {0} , ", i == count - 2 ? array[i] : array[i] * array[i + 1]);
            }
        }
        else
        {
            for (int i = 0; i < array.Length - 1; i += 2)
            {
                Console.Write(" {0} , ", array[i] * array[i + 1]);
            }

        }

答案 1 :(得分:0)

尝试一下:

int[] array = new int[9];
            int userInput = -1;
            int itt = 0;
            int count = 0;

            Console.WriteLine("Enter a integer number or -1 to exit:");
            userInput = Convert.ToInt32(Console.ReadLine());
            count++;
            while (userInput != -1 && itt < array.Length)
            {

                array[itt] = userInput;
                itt++;
                if (count == 9) break;
                Console.WriteLine("Enter a integer number or -1 to exit:");
                userInput = Convert.ToInt32(Console.ReadLine());
                count++;
            }
            Console.WriteLine("The array contains: ");

            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(" {0} , ", array[i]);

            }
            Console.WriteLine("");
            Console.WriteLine("count {0}", count);

            if (count % 2 == 0)
            {
                for (int i = 0; i < array.Length; i += 2)
                {
                    Console.Write(" {0} , ", array[i] * array[i + 1]);
                }
            }
            else
            {
                for (



                    int i = 0; i < array.Length; i += 1)
                {
                    if (i == 7)
                    {
                        Console.Write(" {0} , ", array[i] * array[i + 1]);
                        break;
                    }

                    Console.Write(" {0} , ", array[i] * array[i + 1]);


                }
            }

            Console.ReadLine()

;