C# 返回到我的应用程序中的特定代码块

C# 返回到我的应用程序中的特定代码块,c#,.net,visual-studio,C#,.net,Visual Studio,我是C#的新手,目前正在开发一个简单的ATM应用程序。我试图编写代码,根据用户输入的字母M将其返回主菜单。在我的场景中,break、continue、goto或return关键字似乎不起作用;也许我用错了。下面的陈述正是我想跳到的地方 Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W

我是C#的新手,目前正在开发一个简单的ATM应用程序。我试图编写代码,根据用户输入的字母M将其返回主菜单。在我的场景中,break、continue、goto或return关键字似乎不起作用;也许我用错了。下面的陈述正是我想跳到的地方

Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W1) checking, (W2) saving");
我想从switch语句中嵌套的else if语句中的行jump(下面)跳转到上面的代码部分。我怎样才能做到这一点?感谢您的帮助…谢谢

switch (response)
{
    case "C1": 
        Console.WriteLine("How much would you like to deposit to your checking account?");
        string depositEntry = Console.ReadLine();
        double checkingBalance = Convert.ToInt32(depositEntry) + currentCheckingBalance;
        currentCheckingBalance += checkingBalance;
        Console.WriteLine("Your current checking balance is " + checkingBalance + "\n (X) Exit, (M) Main Menu" );

        string selection = Console.ReadLine().ToUpper();
        if (selection == "X")
        {
            return;
        }
        else if (selection == "M")
        {
            ***JUMP***
        }
        else
        {
            Console.WriteLine("Your entry was invalid");
        }


        break;

    case "C2":      

        break;

    case "W1":

将跳转代码放入函数中并返回

public void MainMenu() {
    // Show the main menu
}

public void Response(string response) {
    switch (response)
    {
        case "C1": 
            Console.WriteLine("How much would you like to deposit to your checking account?");
            string depositEntry = Console.ReadLine();
            double checkingBalance = Convert.ToInt32(depositEntry) + currentCheckingBalance;
            currentCheckingBalance += checkingBalance;
            Console.WriteLine("Your current checking balance is " + checkingBalance + "\n (X) Exit, (M) Main Menu" );

            string selection = Console.ReadLine().ToUpper();
            if (selection == "X")
            {
                return;
            }
            else if (selection == "M")
            {
                ***JUMP***
                MainMenu();
                return;
            }
            else
            {
                Console.WriteLine("Your entry was invalid");
            }


            break;

        case "C2":      

            break;

        case "W1":
    }
}

与已经给出的答案类似,我建议将其打破。下面是一个例子:

Main
方法:

static void Main(string[] args) {
    string input = null;

    do {
        input = Console.ReadLine();
        ParseInput(input);
    } while (input != "X");
}
ParseInput

static void ParseInput(string input) {
    switch (input) {
        case "X": //from Main(), this will close the app
            return;
        case "M":
            MainMenu();
            break;
        case "C1":
            ShowAccount("C1"); //move your deposit/withdraw logic into a method and call with the selected account
            return;
        //other accounts
        default:
            break; //error message?
    }
}
主菜单

static void MainMenu() {
    Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W1) checking, (W2) saving");
}
这将允许您在循环中读取输入,
ParseInput
函数可以处理您的个别情况。您可能还想在开始时调用
main menu()
,因此它会从头开始显示

它的工作原理如下:

  • 从用户处获取输入
  • 将输入传递到
    ParseInput()
    ,由其决定下一步的方向
  • ParseInput()
    中命中的任何函数都将执行,写入控制台或请求进一步输入
  • 一旦该函数返回,
    while(input!=“X”)
    进行计算。如果
    输入!=“X”
    ,转到1,否则退出

  • 我建议您使用
    goto
    C#参考

    static void Main()
    {
        int x = 200, y = 4;
        int count = 0;
        string[,] array = new string[x, y];
    
        // Initialize the array:
        for (int i = 0; i < x; i++)
    
            for (int j = 0; j < y; j++)
                array[i, j] = (++count).ToString();
    
        // Read input:
        Console.Write("Enter the number to search for: ");
    
        // Input a string:
        string myNumber = Console.ReadLine();
    
        // Search:
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                if (array[i, j].Equals(myNumber))
                {
                    goto Found;
                }
            }
        }
    
        Console.WriteLine("The number {0} was not found.", myNumber);
        goto Finish;
    
        Found:
            Console.WriteLine("The number {0} is found.", myNumber);
    
        Finish:
            Console.WriteLine("End of search.");
    
    
        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
    
    static void Main()
    {
    int x=200,y=4;
    整数计数=0;
    字符串[,]数组=新字符串[x,y];
    //初始化阵列:
    对于(int i=0;i
    输入44的输出为:

    • 输入要搜索的号码:44
    • 找到44号了
    • 搜索结束

    有关MSDN参考,请参阅。

    使用跳转语句通常表示逻辑流混乱。如果有必要,我会尽量避免任何形式的跳跃。下面的代码打印出主菜单,如果用户键入“x”,程序将退出。如果用户选择其他选项之一,则只需打印一条消息,指示用户选择的内容。用户按下任意键后,控制台将清除,并重新显示主菜单

    static void Main()
    {
        int x = 200, y = 4;
        int count = 0;
        string[,] array = new string[x, y];
    
        // Initialize the array:
        for (int i = 0; i < x; i++)
    
            for (int j = 0; j < y; j++)
                array[i, j] = (++count).ToString();
    
        // Read input:
        Console.Write("Enter the number to search for: ");
    
        // Input a string:
        string myNumber = Console.ReadLine();
    
        // Search:
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                if (array[i, j].Equals(myNumber))
                {
                    goto Found;
                }
            }
        }
    
        Console.WriteLine("The number {0} was not found.", myNumber);
        goto Finish;
    
        Found:
            Console.WriteLine("The number {0} is found.", myNumber);
    
        Finish:
            Console.WriteLine("End of search.");
    
    
        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
    
    在主菜单中,如果用户未键入其中一个选项,则忽略该选项,清除控制台,并重新打印菜单。没有显示错误,表明选择无效

    这不需要用户键入“m”返回主菜单。选择存款/取款/…方法完成后,代码将自动返回主菜单

    我猜这可能就是你要找的。希望这有帮助

    static void Main(string[] args) {
    
      string userInput = "";
      while ((userInput = GetMainSelection()) != "x") {
        switch (userInput) {
          case "c1":
            Console.WriteLine("C1 Deposit Checking method");
            break;
          case "c2":
            Console.WriteLine("C2 Deposit Savings method");
            break;
          case "b1":
            Console.WriteLine("B1 View Balance Checking method");
            break;
          case "b2":
            Console.WriteLine("B2 View Balance Savings method");
            break;
          case "w1":
            Console.WriteLine("W1 Withdraw Checking method");
            break;
          case "w2":
            Console.WriteLine("W2 withdraw Savings method");
            break;
        }
        Console.WriteLine("Press Any Key to continue"); // <-- show what method was just used
        Console.ReadKey();
        Console.Clear();
      }
      Console.Write("Press any key to exit the program");
      Console.ReadKey();
    }
    
    private static string GetMainSelection() {
      string userInput = "";
      while (true) {
        Console.WriteLine("Select an option? \n VIEW BALANCE (B1) checking, (B2) saving \n DEPOSIT (C1) checking, (C2) saving \n WITHDRAW (W1) checking, (W2) saving. (X) to EXit");
        userInput = Console.ReadLine().ToLower();
        if (userInput == "b1" || userInput == "b2" || userInput == "c1" || userInput == "c2" || userInput == "w1" || userInput == "w2" || userInput == "x") {
          return userInput;
        }
        else {
          Console.Clear();
        }
      }
    }
    
    static void Main(字符串[]args){
    字符串userInput=“”;
    而((userInput=GetMainSelection())!=“x”){
    开关(用户输入){
    案例“c1”:
    控制台写入线(“C1存款检查方法”);
    打破
    案例“c2”:
    控制台。WriteLine(“C2存款储蓄法”);
    打破
    案例“b1”:
    Console.WriteLine(“B1视图余额检查方法”);
    打破
    案例“b2”:
    Console.WriteLine(“B2视图余额节省方法”);
    打破
    案例“w1”:
    控制台写入线(“W1撤消检查方法”);
    打破
    案例“w2”:
    Console.WriteLine(“W2提取储蓄方法”);
    打破
    }
    
    Console.WriteLine(“按任意键继续”);//为什么不干脆中断!我尝试了中断,它返回到if语句块的开头。我想返回到开头。非常感谢。以我目前的技能水平,非常清晰且易于解释。非常感谢你的建议!非常感谢你的建议!非常感谢你的建议!非常感谢你的建议!