c#从函数返回切换大小写

时间:2018-07-19 10:03:51

标签: c#

我是C#的新手,我目前正在做一个作业,我必须创建一个带有开关盒的菜单,其中用户键入A,B,C或D,并且每个都进入华氏温度至摄氏温度转换,分别转换硬币,数字模式或退出菜单。我刚刚完成了华氏转换器的编码(希望正确),但是我需要回到开关盒。

当前我的代码:

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
using System.Threading.Tasks;

namespace cSharp_Assignment
{    
    class Program
    {
        static void Main(string[] args)
        {    
            Console.WriteLine("Type A to go to Fahrenheit Converter");
            Console.WriteLine("Type B to go to Coin Change");
            Console.WriteLine("Type C to go to Number Pattern");
            string menuChoice = Console.ReadLine().ToUpper();

            switch (menuChoice)
            {
                case "A":
                    fahrenheitConverter();
                    break;
                case "B":

                    break;
                case "C":

                    break;
                case "D":
                    break;
            }
        }
        public static void fahrenheitConverter()
        {
            int Fahrenheit = 0;
            Console.WriteLine("Insert a temperature in Fahrenheit");
            Console.ReadLine();
            int Celcius = ((Fahrenheit - 32) * (5 / 9));
            Console.WriteLine("Celcius is: " + Celcius);                
        }
    }
}

4 个答案:

答案 0 :(得分:1)

添加一个while语句,其中包含您想要的退出条件,在您的情况下为“ X”

    while (menuChoice != "X")
    {
        switch (menuChoice)
        {
            case "A":
                fahrenheitConverter();
                break;
            case "B":

                break;
            case "C":

                break;
            case "D":
                break;
        }
        Console.WriteLine("Type A to go to Fahrenheit Converter");
        Console.WriteLine("Type B to go to Coin Change");
        Console.WriteLine("Type C to go to Number Pattern");
        Console.WriteLine("Type X to exit");
        menuChoice = Console.ReadLine().ToUpper();
    }

您的主体应如下所示:

static void Main(string[] args)
{
    Console.WriteLine("Type A to go to Fahrenheit Converter");
    Console.WriteLine("Type B to go to Coin Change");
    Console.WriteLine("Type C to go to Number Pattern");
    Console.WriteLine("Type X to exit");
    string menuChoice = Console.ReadLine().ToUpper();

    while (menuChoice != "X")
    {
        switch (menuChoice)
        {
            case "A":
                fahrenheitConverter();
                break;
            case "B":

                break;
            case "C":

                break;
            case "D":
                break;
        }
        Console.WriteLine("Type A to go to Fahrenheit Converter");
        Console.WriteLine("Type B to go to Coin Change");
        Console.WriteLine("Type C to go to Number Pattern");
        Console.WriteLine("Type X to exit");
        menuChoice = Console.ReadLine().ToUpper();
    }
}

答案 1 :(得分:0)

看看包含退出条件的某种循环...

我不会为您提供c#代码,因为这看起来像是一项作业,但这是伪代码...

while (UpperCase(menuChoice)!="EXIT")
{

            Console.WriteLine("Type A to go to Fahrenheit Converter");
            Console.WriteLine("Type B to go to Coin Change");
            Console.WriteLine("Type C to go to Number Pattern");
            string menuChoice = Console.ReadLine().ToUpper();

            switch (menuChoice)
            {
                case "A":
                    fahrenheitConverter();
                    break;
                case "B":

                    break;
                case "C":

                    break;
                case "D":
                    break;
            }

}

答案 2 :(得分:0)

您非常亲密。您所需要做的就是再添加一个循环,例如

static void Main(string[] args) {
  // Keep on working (execute switch case) ...
  for (bool keepOnWorking = true; keepOnWorking;) {
    Console.WriteLine("Type A to go to Fahrenheit Converter");
    Console.WriteLine("Type B to go to Coin Change");
    Console.WriteLine("Type C to go to Number Pattern");
    Console.WriteLine("Type X to exit");

    // Trim: let us be nice (what if user put/paste "A " or " B"?)
    string menuChoice = Console.ReadLine().Trim().ToUpper();

    switch (menuChoice) {
      case "A":
        fahrenheitConverter();
        break;
      case "B":
        break;
      case "C":
        break;
      case "D":
        break;
      case "X":
        // ... until user press "X". Stop working 
        keepOnWorking = false; 
        break; 
    }
  }  
}

答案 3 :(得分:0)

您可以再次调用main方法。这很容易。它应该工作。但是不推荐。 希望这可以帮助。问候..!

  using System;    
  using System.Collections.Generic;    
  using System.Linq;    
  using System.Text;    
  using System.Threading.Tasks;

  namespace cSharp_Assignment
  {    
  class Program
  {
    static void Main(string[] args)
    {    
        Console.WriteLine("Type A to go to Fahrenheit Converter");
        Console.WriteLine("Type B to go to Coin Change");
        Console.WriteLine("Type C to go to Number Pattern");
        string menuChoice = Console.ReadLine().ToUpper();

        switch (menuChoice)
        {
            case "A":
                fahrenheitConverter();
                break;
            case "B":

                break;
            case "C":

                break;
            case "D":
                break;
        }
    }
    public static void fahrenheitConverter()
    {
        int Fahrenheit = 0;
        Console.WriteLine("Insert a temperature in Fahrenheit");
        Console.ReadLine();
        int Celcius = ((Fahrenheit - 32) * (5 / 9));
        Console.WriteLine("Celcius is: " + Celcius); 

        Main(null); // Just call the main method here. This should work. But not recomended   
    }
}

}