当前位置:编程学习 > C#/ASP.NET >>

请教路过的大神,初识编程1个月,有关条件和各功能的调用问题,感激!

菜鸟新手本人有一个作业,在控制台下的程序集

A> 摄氏、华氏间温度转换器
  1>摄氏转华氏
  2>华氏转摄氏
  3>返回
B> 英尺、米长度转换器
  1>foot&inch to cm
  2>cm to foot&inch
  3>返回
C> 迈、千米/时 速度转换器
  1>kph to mph
  2>mph to kph
  3>返回
D> 油耗计算器
  1>
  2>
  3>返回
E> Exit

(之前发帖问过一次,还被鄙视是野鸡大学。。呃,实在不是,是本人太菜。。现在有一些新的问题)

问题1:如何做到从次级菜单返回主菜单啊?求详细的语句……光知道用循环,不知道咋循环。。
      还有作业要求每执行完一次运算,自动返回次级菜单,这个我也不会。。

问题2:运算中会提示是否显示另外一种计算结果在,这时需要用户输入"Y"来进行,是不是要将这个Y转化成bool来    判断是否进行下一个操作呢?请问如何实现呢?

问题3:用什么样的语句从主菜单调用各个function,这是我写的菜单代码:
 string menu = "\n1> Temperature calculator"
                          + "\n2> Length calculator"
                          + "\n3> Speed calculator"
                          + "\n4> Fuel consuption calculator"
                          + "\n5> Exit program";
            string menuOption,
                   subMenuOption,
                   TempCal,
                   LenthCal,
                   SpdCal,
                   FCCal;   

            Console.Write(menu);
            Console.WriteLine(" \n\nPlease enter your option..");
            menuOption = Console.ReadLine();

            switch (menuOption)
            {
                case "1": menuOption = TempCal ;
                    break;
                case "2": menuOption = LenthCal;
                    break;
                case "3": menuOption = SpdCal;
                    break;
                case "4": menuOption = FCCal;
                    break;
                case "5": return;
                default: Console.WriteLine("Please enter a valid menu option");
                    break;
            }

比如case 4,怎么做可以直接跳到油耗计算器去啊?
各个function之间应当保持什么样的关系呢? 每个function的heading该怎么写? static void什么的吗?


最后是我写的油耗计算器,里面有好多语句不通过>_<! 希望老鸟们猛烈批评指点……

 static void Main(string[] args)
        {
            string readGallonOrLitre,
                   inputFuelUsed,
                   inputDistanceTravelled,
                   seeEqvltResult;
                   
            double fuelUsedGallon,
                   fuelUsedLitre,
                   distanceTravelledMile,
                   distanceTravelledKM,
                   fuelConsptMile,
                   fuelConsptKilo;
            int    
                   gallonOrLitre;
            bool   y;
            const  double KILOS_PER_MILE = 1.61,
                          MILES_PER_KILO = 0.62,
                          GALLON_PER_LITRE = 0.26,
                          LITRE_PER_GALLON = 3.76;

            Console.WriteLine("Please select your prefered measurement...");
            Console.WriteLine("\nFor British imperial measure ( gallon and miles), please enter 1");
            Console.WriteLine("For metric measure ( kilometres and litre), please enter 2");
            Console.Write("\nNow please enter your option: ");

            readGallonOrLitre = Console.ReadLine();
            gallonOrLitre = int.Parse(readGallonOrLitre);

            switch (gallonOrLitre)
            {

                case 1:
                    Console.Write("Please enter the amount of fuel used: ");
                    inputFuelUsed = Console.ReadLine();
                    fuelUsedGallon = double.Parse(inputFuelUsed);

                    Console.Write("Please enter the distance travelled: ");
                    inputDistanceTravelled = Console.ReadLine();
                    distanceTravelledMile = double.Parse(inputDistanceTravelled);
                    fuelConsptMile = distanceTravelledMile / fuelUsedGallon;

                    Console.WriteLine("Fuel consumption is " + fuelConsptMile + " gallons per mile");

                    Console.WriteLine("\nDo you want to see the equivalent result in liters per 100 kilomitres?  \nIf yes enter y; if not enter anything to go back.");

                    seeEqvltResult = Console.ReadLine();
                    y = Convert.ToBoolean(seeEqvltResult);
                    fuelConsptKilo = distanceTravelledMile * KILOS_PER_MILE / fuelUsedGallon * LITRE_PER_GALLON;


                    if (y)
                    {
                        Console.WriteLine("Your current fuel consumption is {0} per 100 kilometres", fuelConsptKilo);

                    }
                    else
                        return;
                    break;

                case 2:
                    Console.Write("Pleased enter the amount of fuel used: ");
                    inputFuelUsed = Console.ReadLine();
                    fuelUsedLitre = double.Parse(inputFuelUsed);

                    Console.Write("Please enter the distance travelled: ");
                    inputDistanceTravelled = Console.ReadLine();
                    distanceTravelledKM = double.Parse(inputDistanceTravelled);
                    fuelConsptKilo = distanceTravelledKM / fuelUsedLitre;

                    Console.WriteLine("Fuel consumption is " + fuelConsptKilo + " litres per kilometre");
                    Console.WriteLine("\nDo you want to see the equivalent result in gallons per 100 miles? \nIf yes enter y; if not enter anything to go back.");

                    seeEqvltResult = Console.ReadLine();
                    y = Convert.ToBoolean(seeEqvltResult);
                    fuelConsptMile = distanceTravelledKM * MILES_PER_KILO / fuelUsedLitre * GALLON_PER_LITRE;

                    if (y)
                    {
                        Console.WriteLine("Your current fuel consumption is {0} per 100 kilometres", fuelConsptMile);
                    }
                    else
                        return;
                    break;


最后的最后,如果有哪位大大愿意无私多帮助我一些,可以具体看看我写好的东西喝作业要求,在这里
https://drive.google.com/folderview?id=0B6tnQDOeCTi0VVJjbVc5Sjk2RTA&usp=sharing
编程 条件 功能 --------------------编程问答-------------------- 拜谢,跪谢!! --------------------编程问答-------------------- 同学,有那么麻烦么。我觉得一屏幕代码就足够了。 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
同学,有那么麻烦么。我觉得一屏幕代码就足够了。


大师!实在是新手,理念能明白,伪代码能看懂,就是憋不出来。。。
要是能劳烦给写一遍,绝对恍然大悟啊,求指点。。。 --------------------编程问答-------------------- 这是你写的第几个程序啊? --------------------编程问答--------------------
引用 4 楼 u010373015 的回复:
这是你写的第几个程序啊?

处女程序。。。。。。。。。。。。。 --------------------编程问答-------------------- 我只是逛逛论坛,不会C#。同求大神出来帮你解答。 --------------------编程问答-------------------- 没注释,无语 --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Command
    {
        public char CommandKey { get; set; }
        public string CommandName { get; set; }
        public string InputPrompt { get; set; }
        public string OutputPrompt { get; set; }
        public Func<double, double> Action { get; set; }
    }

    class Menu
    {
        public char CommandKey { get; set; }
        public string CommandName { get; set; }
        public List<Command> Commands { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Menu> mainMenu = new List<Menu>()
            {
                new Menu()
                {
                    CommandKey = 'A',
                    CommandName = "摄氏、华氏间温度转换器",
                    Commands = new List<Command>() 
                    { 
                        new Command() { Action = x => 32.0 + x * 1.8, CommandKey = '1',
                                        CommandName = "摄氏转华氏", InputPrompt = "请输入摄氏温度:", 
                                        OutputPrompt = "{0}C = {1}F." },
                        new Command() { Action = x => (x - 32.0) / 1.8, CommandKey = '2',
                                        CommandName = "华氏转摄氏", InputPrompt = "请输入华氏温度:", 
                                        OutputPrompt = "{0}F = {1}C." }
                    }
                },
                new Menu()
                {
                    CommandKey = 'B',
                    CommandName = "英尺、米长度转换器",
                    Commands = new List<Command>() 
                    { 
                        new Command() { Action = x => x * 2.54, CommandKey = '1',
                                        CommandName = "foot&inch to cm", InputPrompt = "请输入inch:", 
                                        OutputPrompt = "{0}inch = {1}cm." },
                        new Command() { Action = x => x / 2.54, CommandKey = '2',
                                        CommandName = "cm to foot&inch", InputPrompt = "请输入cm:", 
                                        OutputPrompt = "{0}cm = {1}inch." }
                    }
                },
                new Menu()
                {
                    CommandKey = 'C',
                    CommandName = "迈、千米/时 速度转换器",
                    Commands = new List<Command>() 
                    { 
                        new Command() { Action = x => x * 1.609344, CommandKey = '1',
                                        CommandName = "mph -> kmph", InputPrompt = "请输入mph:", 
                                        OutputPrompt = "{0}mph = {1}kmph." },
                        new Command() { Action = x => x / 1.609344, CommandKey = '2',
                                        CommandName = "kmph -> mph", InputPrompt = "请输入kmph:", 
                                        OutputPrompt = "{0}kmph = {1}mph." }
                    }
                },
                new Menu()
                {
                    CommandKey = 'D',
                    CommandName = "油耗计算器",
                    Commands = new List<Command>()
                }
            };
            string inputKey = "";
            char exitKey = (char)(mainMenu.Max(x => x.CommandKey) + 1);
            while (inputKey != exitKey.ToString())
            {
                Console.Clear();
                Console.WriteLine("=====Main Menu=====");
                foreach (var menu in mainMenu)
                {
                    Console.WriteLine("{0}> {1}", menu.CommandKey, menu.CommandName);
                }
                Console.WriteLine("{0}> {1}", exitKey, "Exit");
                inputKey = Console.ReadLine().ToUpper();
                if (mainMenu.Any(x => x.CommandKey == inputKey.FirstOrDefault()))
                {
                    Menu menu = mainMenu.First(x => x.CommandKey == inputKey.FirstOrDefault());
                    char returnKey = (char)(menu.Commands.Select(x => x.CommandKey).DefaultIfEmpty('0').Max() + 1);
                    inputKey = "";
                    while (inputKey != returnKey.ToString())
                    {
                        Console.Clear();
                        foreach (var cmd in menu.Commands)
                        {
                            Console.WriteLine("{0}> {1}", cmd.CommandKey, cmd.CommandName);
                        }
                        Console.WriteLine("{0}> {1}", returnKey, "Return");
                        inputKey = Console.ReadLine().ToUpper();
                        if (menu.Commands.Any(x => x.CommandKey == inputKey.FirstOrDefault()))
                        {
                            Command cmd = menu.Commands.First(x => x.CommandKey == inputKey.FirstOrDefault());
                            Console.Write(cmd.InputPrompt);
                            double input = double.Parse(Console.ReadLine());
                            Console.WriteLine(cmd.OutputPrompt, input, cmd.Action(input));
                            Console.ReadKey();
                            inputKey = returnKey.ToString();
                        }
                    }
                    inputKey = "";
                }
            }
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,