使用C#将数组中的交替数字相乘

时间:2016-06-20 13:22:57

标签: c# arrays multiplication

我正在做一个学校项目,我必须在给出前七位数时找到一个GTIN代码,这里是代码:

Console.WriteLine("Please enter the first 7 digits of the code");// asking the user to input the digits of the code
            int[] GTIN = new int[7];//creating the integer array GTIN to hold the first 7 digits
            GTIN[0] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the first digit of the code and multiplies it by 3
            GTIN[1] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the second digit of the code and multiplies it by 1
            GTIN[2] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the third digit of the code and multiplies it by 3
            GTIN[3] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the fourth digit of the code and multiplies it by 1
            GTIN[4] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the fith digit of the code and multiplies it by 3
            GTIN[5] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the sixth digit of the code and multiplies it by 1
            GTIN[6] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the seventh digit of the code and multiplies it by 3

            int sum = GTIN.Sum();// adds up the product of the multiplications and stores them in int sum
            Console.WriteLine("The total is " + sum);//prints the total to the console
            int roundup = ((sum - sum % 10) + 10);// takes the sum and rounds it up to the nearest 10 and stores it in int roundup
            Console.WriteLine(roundup);// prints roundup
            int finaldigit = roundup - sum;// woks out the final digit of the code
            Console.WriteLine("The eighth digit of your code is " + finaldigit);// prints the final digit

            Console.WriteLine("Enter the code you want to verify");//asks the user to input the 8 digit code that they want to verify
            int[] GTIN2 = new int[8];//creating the integer array GTIN2 to hold the first 8 digits
            GTIN2[0] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the first digit of the code and multiplies it by 3
            GTIN2[1] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the second digit of the code and multiplies it by 1
            GTIN2[2] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the third digit of the code and multiplies it by 3
            GTIN2[3] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the fourth digit of the code and multiplies it by 1
            GTIN2[4] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the fith digit of the code and multiplies it by 3
            GTIN2[5] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the sixth digit of the code and multiplies it by 1
            GTIN2[6] = Convert.ToInt32(Console.ReadLine()) * 3;//takes the seventh digit of the code and multiplies it by 3
            GTIN2[7] = Convert.ToInt32(Console.ReadLine()) * 1;//takes the eighth digit of the code and multiplies it by 1

            int sum2 = GTIN2.Sum();// adds up the product of the multiplications and stores them in int sum2
            Console.WriteLine("The total is " + sum2);// prints the total of the sum

            if (sum2 % 5 == 0)// opens if statement; if the remainder of sum2 divided by 5 ends in a 0 then...
            {
                Console.WriteLine("Your code has been verified");//print your code has been verifed 
            }
            else// else
            {
                Console.WriteLine("The code is incorrect");// print code is incorrect
            }

            Console.ReadLine();//end

正如你所看到的,这有效,但效率不高,我需要找出如何将乘法作为一个循环,但我不知道如何。提前谢谢。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情,

int sum = 0,value=0;
for(int i=0;i<GTIN.Length;i++) //allows you to fill the array
{
    Console.WriteLine("Please enter an integer");
    if((i%2)==0)
      value = 3;
    else
      value = 1;
    sum += value * int.Parse(Console.ReadLine()); 
}

答案 1 :(得分:0)

sum *= num[i];将完成您的任务。

参考 - This Stack Overflow