0

ピザの直径を取得し、保持できるスライスの数と各スライスの面積、ピザを見つけるプログラムを構築する必要があります。定数でピザのサイズを定義しますが、「int numberOfSlice」でエラーが発生します割り当てられていないローカル変数の使用を言っていますが、ifステートメントを割り当てていると思っていました。

class Program
{
    static void Main(string[] args)
    {
        //declarations of Constans

        const int SMALL_MIN = 12; 
        const int SMALL_MED = 16;
        const int MED_LARGE = 24;
        const int LARGE_XLARGE = 30;
        const int XL_MAX = 36;
        const int SMALL_SLICE = 8;
        const int MED_SLICE = 12;
        const int LARGE_SLICE = 16;
        const int XL_SLICES = 24;
        //declarations of varable
        double pizzaDiameter;
        int numberOfSlices = 0;
        double sliceArea;
        double radius; 
        string userInput = " ";


        Console.WriteLine("Please enter the diameter of your pizza:"); // tell user to input diameter 
        userInput = Console.ReadLine(); // gets userinput
        double.TryParse(userInput, out pizzaDiameter); // see if userinput is vaild


        if (pizzaDiameter >= SMALL_MIN && pizzaDiameter <= XL_MAX) // if in range will continue
        {

            // all the ranges for the pizzas 
            if (pizzaDiameter >= SMALL_MIN && pizzaDiameter < SMALL_MED)
            {
                numberOfSlices = (SMALL_SLICE);
            }

            else if (pizzaDiameter >= SMALL_MED && pizzaDiameter < MED_LARGE)
            {
                numberOfSlices = (MED_SLICE);
            }

            else if (pizzaDiameter >= MED_SLICE && pizzaDiameter < LARGE_XLARGE)
            {
                numberOfSlices = (LARGE_SLICE);
            }

            else if (pizzaDiameter >= LARGE_XLARGE && pizzaDiameter <= XL_MAX)

            {
                numberOfSlices = (XL_SLICES);
            }


            radius = pizzaDiameter / 2; // divides pizzaDiameter to get radius 


            sliceArea = Math.PI * Math.Pow(radius, 2) / numberOfSlices; // gets slice area

            sliceArea = Math.Round(sliceArea, 2); // rounds to 2 places

            // output of resluts 
            Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield " + numberOfSlices + " slices.");
            Console.WriteLine("\nEach slice will have an area of " + sliceArea + "\".");

            Console.WriteLine("\nPress any key to exit..."); // tells user to end program
            Console.ReadKey(); // readkey to end program


        }



        else // if the diameter was not in range will display this error 
        {
            Console.WriteLine("\nEntry Error ");
            Console.WriteLine("\nPizza must have a diameter in the range of 12\" to 36\" inclusive!");
            Console.WriteLine("please try again");

            Console.WriteLine("\nPress any key to end this application...");// tells user to end program
            Console.ReadKey(); // readkey to end program

        }      






    }
}

}

4

4 に答える 4