3合1方法c#控制台应用程序

本文关键字:控制台 应用程序 1方法 | 更新日期: 2023-09-27 18:19:09

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Combine
{
    class Program 
    { 
        public void marks()
        {
            int marks;
            string Grade = null; // The initial value of grade is 0.
            //Prompt User
            Console.WriteLine("Please enter your mark here");
            marks = int.Parse(Console.ReadLine());
            if (marks < 0 || marks > 100)
            {
                Console.WriteLine("mark entered is not valid");
            }
            else
            {
                if (marks >= 85 && marks <= 100)
                {
                    Grade = "A";
                }
                else if (marks >= 70 && marks <= 84)
                {
                    Grade = "B";
                }
                else if (marks >= 60 && marks <= 69)
                {
                    Grade = "C";
                }
                else if (marks >= 50 && marks <= 59)
                {
                    Grade = "D";
                }
                else if (marks >= 0 && marks <= 49)
                {
                    Grade = "F";
                }
                Console.WriteLine("grade is a " + Grade + " grade");
            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // INTRO TO APP //
            string b;
            string bselect;
            Program p = new Program();
            Console.WriteLine("Welcome");
            Console.WriteLine("Please Select A Menu Item");
            bselect = Console.ReadLine();
            b = bselect;
            if (bselect == b )
                Console.WriteLine("B - Body Mass Index")
        }
        {
            Console.WriteLine("L - Letter Grade");
            string lselect;
            string l;
            Program p = new Program();
            lselect = Console.ReadLine();
            l = lselect;
            p.marks();
        }
        {
            string t;
            Console.WriteLine("T - Tax Due");
            t = Console.ReadLine();
        }
        {
            string x;
            Console.WriteLine("X - Exit");
            x = Console.ReadLine();
        }
    }
}
}

为此,我试图将三个控制台应用程序合并为一个简单的菜单选项,以选择运行哪个应用程序。我知道我应该使用方法,但我不确定如何正确地做它们。我用谷歌搜索过,但没有一条信息我能真正理解。谢谢。

3合1方法c#控制台应用程序

我想我明白你的意思了。根据您提供的代码示例,您可能需要学习一些基础知识。我希望这能让你再次行动起来。

namespace Combine
{
    using System;
    public class LetterGradeProgram
    {
        public void Execute()
        {
            int marks;
            string grade = null;
            Console.WriteLine("Please enter your mark here");
            marks = int.Parse(Console.ReadLine());
            if (marks < 0 || marks > 100)
            {
                Console.WriteLine("mark entered is not valid");
            }
            else
            {
                if (marks >= 85 && marks <= 100)
                {
                    grade = "A";
                }
                else if (marks >= 70 && marks <= 84)
                {
                    grade = "B";
                }
                else if (marks >= 60 && marks <= 69)
                {
                    grade = "C";
                }
                else if (marks >= 50 && marks <= 59)
                {
                    grade = "D";
                }
                else if (marks >= 0 && marks <= 49)
                {
                    grade = "F";
                }
                Console.WriteLine("grade is a " + grade + " grade");
            }
            Console.ReadLine();
        }
    }
    public class BodyMassIndexProgram
    {
        public void Execute()
        {
            // do something here
        }
    }
    public class TaxDueProgram
    {
        public void Execute()
        {
            // do something here
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Welcome");
            Console.WriteLine("Please Select A Menu Item");
            Console.WriteLine("B - Body Mass Index");
            Console.WriteLine("T - Tax Due");
            Console.WriteLine("L - Letter Grade");
            string selected = Console.ReadLine();
            switch (selected.ToUpperInvariant())
            {
                case "B":
                    BodyMassIndexProgram bodyMassIndex = new BodyMassIndexProgram();
                    bodyMassIndex.Execute();
                    break;
                case "T":
                    TaxDueProgram taxDue = new TaxDueProgram();
                    taxDue.Execute();
                    break;
                case "L":
                    LetterGradeProgram letterGrade = new LetterGradeProgram();
                    letterGrade.Execute();
                    break;
                default:
                    Console.WriteLine("Unknown option {0}", selected);
                    break;
            }
            Console.WriteLine("Bye");
        }
    }
}