Im ein Schulprojekt zu tun in dem ich habe einen GTIN Code zu finden, wenn die ersten sieben Ziffern angegeben, hier ist der Code:Multiply alternative Stellen in einem Array mit C#
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
Wie können Sie diese sehen funktioniert, aber es ist nicht effizient, ich muss herausfinden, wie man die Multiplikation als Schleife macht, aber ich weiß nicht wie. Danke im Voraus.