lundi 16 avril 2018

If Else Statment not working?

I'm new to C# and would like some help!

I am working on some code that allows the user to find out if specific cows on his farm aren't producing enough milk.

On line 75 the if statement is supposed to print out the cows that 'are not good enough' or Tell the user that everything is OK. But instead it permanently try's to print the Bad Cows.

        Console.WriteLine("How many cows are in your herd?");
        int CowNum = int.Parse(Console.ReadLine());

        int Temp;
        double TempD;
        string TempS;
        int MinimumVal = 6;
        int MDIR = 4;

        string[] BadList = new string[CowNum];
        int[] Counter = new int[CowNum];
        double[] Total = new double[CowNum];
        string[] Days = new string[7] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
        string[] CowID = new string[CowNum];
        double[,] CowYield = new double[CowNum, 7];


        Random r = new Random();

        for (int n = 0; n < CowNum; n++) // Sets Cow ID's
        {
            Console.WriteLine("What is the ID of Cow: " + (n + 1) + "?" );
            TempS = (Console.ReadLine());
            CowID[n] = TempS;
        }

        for (int n = 0; n < CowNum; n++) // Sets the Yield of each cow to a certin day
            for (int x = 0; x < Days.GetLength(0); x++)
            {
                Console.WriteLine("What was the Yeild for Cow: " + CowID[n] + " on " + Days[x] + "?");
                TempD = double.Parse(Console.ReadLine());
                CowYield[n, x] = TempD;
                if (TempD < MinimumVal)
                {
                    Counter[n] = Counter[n] + 1;
                    Console.WriteLine(Counter[n]);
                }

                //Console.WriteLine("What was the Yeild for Cow: " + CowID[n] + " on " + Days[x] + "?"); //Randomly Generated 'Saves Time'
                //Temp = r.Next(0, 20);
                //Console.WriteLine(Temp);
                //CowYield[n, x] = Temp;
            }

        for (int n = 0; n < CowNum; n++)
            for (int x = 0; x < Days.GetLength(0); x++)
                Total[n] = Total[n] + CowYield[n, x];

        for (int n = 0; n < CowNum; n++)
        {
            if (Counter[n] > MDIR)
            {
                BadList[n] = CowID[n];
                Console.WriteLine("asd" + BadList[n]);
            }
        }


                int index = Array.IndexOf(Total, Total.Max()); // Gets index of Highest producing cow
        TempS = CowID[index];
        Console.WriteLine("\nThe Highest producing cow is Cow: " + TempS + ". with a whopping " + Total[index] + "L of Milk!\n");

        if (BadList.GetLength(0) > 0)
        {
            Console.WriteLine(BadList.GetLength(0));
            for (int n = 0; n < BadList.GetLength(0); n++)
            {
                Console.WriteLine(BadList[n]);
            }
        }
        else
        {
            Console.WriteLine("None of your cows had less than 6L of milk for four or more days in a row!");
        }



        Console.ReadLine();
    }
}

}

Aucun commentaire:

Enregistrer un commentaire