C#控制台应用程序计算循环

C#控制台应用程序计算循环,c#,loops,console-application,calculator,C#,Loops,Console Application,Calculator,嗨,这只是一个简单的计算器。在我询问用户是否要进行另一次转换后,我希望允许用户输入“N”或“N”。 (输入Y进行另一次转换/输入N返回主菜单)。我该怎么做 static int LengthCalculator() { int LengthCalculatorOption; string AnotherConversion = null; double Centimetres = 0.0, Feet = 0.0, Inches =

嗨,这只是一个简单的计算器。在我询问用户是否要进行另一次转换后,我希望允许用户输入“N”或“N”。 (输入Y进行另一次转换/输入N返回主菜单)。我该怎么做

        static int LengthCalculator() {

        int LengthCalculatorOption;
        string AnotherConversion = null;
        double Centimetres = 0.0, Feet = 0.0, Inches = 0.0, TotalInches = 0.0;
        const double CENTIMETRES_PER_INCH = 2.54, INCHES_PER_FOOT = 12;

        do {
            LengthCalculatorMenu();
            LengthCalculatorOption = ValidLengthCalculatorReadOption();

            if (LengthCalculatorOption == 1) {
                Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches:");
                Centimetres = double.Parse(Console.ReadLine());
                TotalInches = (Centimetres / CENTIMETRES_PER_INCH); // This will take a floor function of Centimetres/2.54
                Feet = (TotalInches - TotalInches % INCHES_PER_FOOT) / INCHES_PER_FOOT; // This will make it divisible by 12
                Inches = TotalInches % INCHES_PER_FOOT; // This will give you the remainder after you divide by 12
                Console.WriteLine("\nThe equivalent in feet and inches is {0} ft {1} ins", Feet, Inches);
                Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any key return to Main menu):");
                AnotherConversion = Console.ReadLine();
            } else if (LengthCalculatorOption == 2) {
                Console.WriteLine("Please Enter the Feet:");
                Feet = double.Parse(Console.ReadLine());
                Console.WriteLine("Please Enter the Inches:");
                Inches = double.Parse(Console.ReadLine());
                Centimetres = ((Feet * INCHES_PER_FOOT) + Inches) * CENTIMETRES_PER_INCH;
                Console.WriteLine("\nThe equivalent in centimetres is {0}cm", Centimetres);
                Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any key return to Main menu):");
                AnotherConversion = Console.ReadLine();
            }
        } while (AnotherConversion == "y" || AnotherConversion == "Y");





        return LengthCalculatorOption;
    }//End LenthCalculator

    static void LengthCalculatorMenu() {
        string LengthCalculatorMenu = ("Enter 1) Convert Centimetres to Feet and Inches:"
                                    + "\nEnter 2) Convert feet and inches to centimetres:");
        Console.WriteLine(LengthCalculatorMenu);
    } // End LengthCalculatorMenu

    static int ValidLengthCalculatorReadOption() {

        int LengthCalculatorOption;
        bool ValidLengthCalculatorOption = false;

        do {
            LengthCalculatorOption = int.Parse(Console.ReadLine());

            if ((LengthCalculatorOption >= 1) && (LengthCalculatorOption <= 2)) {
                ValidLengthCalculatorOption = true;
            } else {
                ValidLengthCalculatorOption = false;
            } // end if

            if (!ValidLengthCalculatorOption) {
                Console.WriteLine("\n\t Option must be 1 or 2, Please Re-Enter your Option");
                LengthCalculatorMenu();
            } //end if 

        } while (!ValidLengthCalculatorOption);

        return LengthCalculatorOption;
    }// End LengthCalculatorReadOption


    static int ReadMainMenuOption() {
        int option = 0;
        bool ValidMainMenuOption = false;

        do {
            option = int.Parse(Console.ReadLine());

            if ((option >= 1) && (option <= 5)) {
                ValidMainMenuOption = true;
            } else {
                ValidMainMenuOption = false;
            } // end if

            if (option == 1) {
                LengthCalculator();
            } else if (option == 2) {

            } else if (option == 3) {

            } else if (option == 4) {

            } else if (option == 5) {

            } // end if 

            if (!ValidMainMenuOption) {
                Console.WriteLine("\n\t\a Option must be 1,2,3,4 or 5");
                DisplayMenu();
            } //end if 
        } while (!ValidMainMenuOption);

        return option;
    } //end ReadOption

    /* Displays Main Menu
     * Precondition:true
     * postcondition: DisplayMenu displayed
     */
    static void DisplayMenu() {
        string mainMenu = "\n1)Length Calculator"
                        + "\n2)Body Mass Index Calculator"
                        + "\n3)Waist to Height Calculator"
                        + "\n4)Fuel Consumption Calculator"
                        + "\n5)Exit the Calculator"
                        + "\n\nEnter your option(1,2,3,4 or 5 to exit):";
        Console.Write(mainMenu);
    } //end DisplayMenu



    static void Main(string[] args) {
        const int Exit = 5;
        int menuOption;
        do {
            DisplayMenu();
            menuOption = ReadMainMenuOption();
        } while (menuOption != Exit);
        Console.Write("Thank you for using this Calculator. Press any Key to Exit");
        //terminating message 
        Console.ReadKey();

    }//end Main
静态整数长度计算器(){
int长度计算器选项;
字符串AnotherConversion=null;
双厘米=0.0,英尺=0.0,英寸=0.0,总英寸=0.0;
常数双厘米每英寸=2.54,英寸每英尺=12;
做{
长度计算器菜单();
LengthCalculatorOption=ValidLengthCalculatorReadOption();
如果(长度计算器选项==1){
Console.WriteLine(“请输入希望转换为英尺和英寸的厘米:”;
厘米=double.Parse(Console.ReadLine());
TotalInches=(厘米/厘米/英寸);//这将采用厘米/2.54的地板功能
英尺=(TotalInches-TotalInches%INCHES\u PER\u FOOT)/INCHES\u PER\u FOOT;//这将使其可被12整除
Inches=TotalInches%Inches\u PER\u FOOT;//这将得到除以12后的余数
Console.WriteLine(“\n英尺和英寸的等价物为{0}英尺{1}英寸”,英尺,英寸);
控制台。写入(“\n是否要进行另一次转换?\n\n(输入Y进行另一次转换/输入任意键返回主菜单):”;
AnotherConversion=Console.ReadLine();
}else if(LengthCalculator选项==2){
控制台。WriteLine(“请输入脚:”);
Feet=double.Parse(Console.ReadLine());
Console.WriteLine(“请输入英寸:”);
Inches=double.Parse(Console.ReadLine());
厘米=((英尺*英寸每英尺)+英寸)*厘米每英寸;
Console.WriteLine(“\n以厘米表示的等价物为{0}厘米”,厘米);
控制台。写入(“\n是否要进行另一次转换?\n\n(输入Y进行另一次转换/输入任意键返回主菜单):”;
AnotherConversion=Console.ReadLine();
}
}而(另一个转换==“y”|另一个转换==“y”);
返回长度计算器选项;
}//末端长度计算器
静态空隙长度计算器菜单(){
字符串长度计算器菜单=(“输入1)将厘米转换为英尺和英寸:
+“\n输入2)将英尺和英寸转换为厘米:”;
控制台写入线(长度计算器菜单);
}//结束长度计算器菜单
静态int ValidLengthCalculatorReadOption(){
int长度计算器选项;
bool ValidLengthCalculatorOption=false;
做{
LengthCalculatorOption=int.Parse(Console.ReadLine());

如果((LengthCalculatorOption>=1)&&(LengthCalculatorOption=1)&&(option在所需位置调用此方法:

static bool shouldMakeAnotherConversion()
{
    repeatQuestion:
    // This shows the question to the user
    Console.Write("Do you want to make another conversion (Y/N)? ");
    ConsoleKeyInfo answer = Console.ReadKey(true);
    switch (answer.Key)
    {
        case ConsoleKey.Y: return true;
        case ConsoleKey.N: return false;
    }
    //If the user types any other key, the program will repeat the question
    Console.WriteLine();
    goto repeatQuestion;
}

您可以创建一个单独的方法来处理用户输入。例如,此方法将确定用户是否输入了Y或N。如果他们没有输入,则会重新提示他们这样做:

static bool AnotherConversion()
{
    var prompt = "\nWould you like to make an another conversion? \n\n(Enter (Y) to make another conversion or (N) to return to the Main Menu):";
    Console.WriteLine(prompt);

    while (true)
    {
        var userInput = Console.ReadLine();

        if (String.Compare("Y", userInput, StringComparison.Ordinal)
        {
            return true;
        }
        else if (String.Compare("N", userInput, StringComparison.Ordinal)
        {
            return false;
        }
        else
        {
            // Invlalid input, re-prompt
            Console.WriteLine("Invalid Input, please enter or (Y) or (N)!");
            Console.WriteLine(prompt);
        }
    }
}
您可以简单地更新do/while循环,使条件基于另一个转换方法。这将允许在计算完成时询问提示:

static int LengthCalculator() {
    ....
    do {
         .....
    } while (AnotherConversion());
    return LengthCalculatorOption;
}//End LenthCalculator

}

你试过使用程序员最好的朋友吗?他的名字叫Debuggernop never我对编程很陌生。我刚开始一周
class Program
{
    public static void parameter(int num1, int num2, out int add, out int sub, out int mul, out float div)
    {
        add = num1 + num2;
        sub = num1 - num2;
        mul = num1 * num2;
        div = (float)num1 / num2;
    }
    static void Main(string[] args)
    {
        int num1, num2;
        int add, sub, mul;
        float div;
        Console.Write("Enter 1st number\t");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("\nEnter 2nd number\t");
        num2 = Convert.ToInt32(Console.ReadLine());

        Program.parameter(num1, num2, out add, out sub, out mul, out div);
        Console.WriteLine("\n\n{0} + {1} = {2}", num1, num2, add);
        Console.WriteLine("{0} - {1} = {2}", num1, num2, sub);
        Console.WriteLine("{0} * {1} = {2}", num1, num2, mul);
        Console.WriteLine("{0} / {1} = {2}", num1, num2, div);

        Console.ReadLine();
    }
}
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Type you first number :");

            Console.WriteLine("Type you second number :");


            Console.WriteLine("Enter the operation + (addition), - (soustraction), * (multiplication), / (division)");
            string stringOperation = Console.ReadLine();


            switch (operation)
            {
                case 1:
                    result = firstNumber + secondNumber;
                    break;

                case 2:
                    result = firstNumber - secondNumber;
                    break;

                case 3:
                    result = firstNumber * secondNumber;
                    break;

                case 4:
                    result = firstNumber / secondNumber;
                    break;

               }

        }
    }
}