【发布时间】: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