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();
}
}
}
}
为此,我尝试将三个控制台应用程序合并为一个简单的菜单选项,以选择要运行的应用程序。我知道我应该使用方法,但我不确定如何正确地做到这些。我用谷歌搜索,但没有一个我能真正理解的信息。感谢。
答案 0 :(得分:-1)
我想我明白你在问什么。根据您提供的代码示例,您可能需要学习很多基础知识。我希望这会让你再次感动......
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");
}
}
}