【问题标题】:Change variable within a C# 'switch' statement在 C# 'switch' 语句中更改变量
【发布时间】:2014-09-10 22:12:51
【问题描述】:

对于学校作业,我应该创建一个类似于 ATM 的菜单。

我的教授给了我们这个代码供我们使用:

string choice = null;

do
{
    Console.Write("[O]pen Account [I]nquire [D]eposit [W]ithdraw [Q]uit: ");
    choice = Console.ReadLine();
    choice = choice.ToUpper();

    switch (choice)
    {
        case "O": // open an account
        case "I": // inquire
        case "D": // deposit
        case "W": // withdraw
        default: break;
    }
} while (choice != "Q");

这是我所做的:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string choice = null;
            string CustomerName;

            Console.WriteLine("Welcome to Fantasy Bank");
            Console.Write("Please enter your name:");
            CustomerName = Console.ReadLine();
            do
            {
                Console.WriteLine("What can I do for you");
                Console.Write("[O]pen Account [I]nquire [D]eposit [W]ithdraw [Q]uit: ");
                choice = Console.ReadLine();
                choice = choice.ToUpper();

                double CurrentBalance = 0;
                switch (choice)
                { 

                    case "O": // open an account
                        Console.Write("Name of account holder:");
                        Console.WriteLine(CustomerName);
                        Console.Write("Initial Deposit:");
                        CurrentBalance = Convert.ToDouble(Console.ReadLine());  // i get a major error if someone types in a letter instead of a number
                        Console.Write("You have succesfully opened an account with an initial deposit of ");
                        Console.Write(CurrentBalance);
                        Console.WriteLine(" at an imaginary bank. Congratulations");
                    break;
                    case "I": // inquire
                        Console.Write(CustomerName);
                        Console.WriteLine("'s Bank Account");
                        Console.WriteLine(CurrentBalance);
                    break;

我做了更多,但问题从case "I" 开始。 CustomerName 被用户键​​入的内容所取代,就像它应该是的那样。但是CurrentBalance 没有改变,我必须将它设置为等于某个值,否则我会出错。

我开始觉得在switch 中更改switch 变量可能是不可能的。我在我的书中查看了传递引用/值,但它不包括该部分中的switch。 如果你们都可以给我一个提示我做错了什么或者可以告诉我什么可以解决我的问题,那就太好了。我不期待你的代码,只是朝着正确的方向前进。

【问题讨论】:

    标签: c# visual-studio switch-statement


    【解决方案1】:

    您的问题是您的CurrentBalance 声明的位置

    目前你有这个:

    do
    {
        double CurrentBalance = 0;
        switch (choice) {
            /* the rest of your code */
        }
    }
    

    应该是

    double CurrentBalance = 0;
    do
    {
        switch (choice) {
            /* the rest of your code */
        }
    }
    

    现在,do 循环的下一次迭代不会将 CurrentBalance 重置为 0

    【讨论】:

      【解决方案2】:

      您将CurrentBalance 重置为0 的循环的每次迭代。移动double CurrentBalance = 0; 行:

      string choice;
      string CurrentName;
      double CurrentBalance = 0;
      
      // ...
      
      do
      {
          // ...
          //double CurrentBalance = 0; NOT HERE
          switch( ... )
          {
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        您应该在进入循环之前初始化所有变量,而不是在循环中,否则每次迭代都会重新初始化变量(清除为0)。

        double CurrentBalance = 0;
        // other code...
        do { // ...
        

        我应该提到它与更改开关中的变量没有任何关系。在 switch 中更改变量是完全允许的。

        【讨论】:

          猜你喜欢
          • 2023-03-28
          • 2019-04-01
          • 2011-12-19
          • 1970-01-01
          • 1970-01-01
          • 2011-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多