un *_*own 3 c# switch-statement visual-studio
对于学校作业,我应该创建一个类似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");
Run Code Online (Sandbox Code Playgroud)
这是我做的:
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;
Run Code Online (Sandbox Code Playgroud)
我做了一点点,但问题从这里开始case "I".CustomerName正在被用户输入的东西所取代,就像它应该是的那样.但是CurrentBalance不会改变,我必须将它设置为等于某些东西,否则我会收到错误.
我开始觉得改变一个switch变量是不可能的switch.我查看了我的书中传递引用/值,但它没有包含switch在该部分中.如果你们都能给我一个暗示我做错了什么或者能告诉我什么可以解决我的问题,那就太好了.我不期待你的代码,只是向正确的方向推进.
您的问题是您的声明的放置CurrentBalance.
目前你有这个:
do
{
double CurrentBalance = 0;
switch (choice) {
/* the rest of your code */
}
}
Run Code Online (Sandbox Code Playgroud)
应该
double CurrentBalance = 0;
do
{
switch (choice) {
/* the rest of your code */
}
}
Run Code Online (Sandbox Code Playgroud)
现在,do循环的下一次迭代不会重置CurrentBalance为0
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |