C# 我如何让这个程序允许输入小数?

C# 我如何让这个程序允许输入小数?,c#,decimal,C#,Decimal,我有一个问题,我必须为学校的作业做。我很难让它与小数一起工作。如果有人能帮我让程序接受小数。它将输出一个十进制数,但不允许我输入一个 using System; public class Bank_Account { public static void Main() { char input, C, D, P, Q; double balance; Console.Wri

我有一个问题,我必须为学校的作业做。我很难让它与小数一起工作。如果有人能帮我让程序接受小数。它将输出一个十进制数,但不允许我输入一个

using System;

    public class Bank_Account
    {
        public static void Main()
        {
            char input, C, D, P, Q;
            double balance;

            Console.WriteLine("Please enter C for Cheque, D for Deposit, P for Print or Q to quit");
            input = Convert.ToChar(Console.ReadLine());
            do
            {
                switch (input)
                {
                    case 'D':
                        Deposit(ref balance);
                        break;
                    case 'P':
                        Print(balance);
                        break;
                    case 'C':
                        Cheque(ref balance);
                        break;
                    default:
                        Console.WriteLine("{0} is not a valid choice", input);
                        break;
                }
            } while (input != 'Q');

        }

        public static void Deposit(ref double balance)
        {
            double deposit;

            Console.WriteLine("Enter the amount you want to deposit: ");
            deposit = Convert.ToInt32(Console.ReadLine());

            if (deposit > 1000)
            {
                deposit = deposit*1.02;
            }
            else
            {
                deposit = deposit;
            }

            balance = +deposit;
        }


        public static void Cheque(ref double balance)
        {
            var tempBalance = balance;
            double deduction;
            const double fee = 1;


            do
            {
                Console.WriteLine("Enter the amount of the cheque: ");
                deduction = Convert.ToInt32(Console.ReadLine());
            } while (deduction < 0);


            tempBalance = balance - deduction - fee;
            if (tempBalance < 0)
            {
                Console.Write("Not enough money in the account");
            }
            else
            {
                balance = tempBalance;
            }
        }


        public static void Print(double balance)
        {
            Console.WriteLine("Your current balance is {0.00}", balance);
        }
    }
使用系统;
公营银行户口
{
公共静态void Main()
{
字符输入,C,D,P,Q;
双平衡;
Console.WriteLine(“请输入C表示支票,输入D表示存款,输入P表示打印或输入Q表示退出”);
input=Convert.ToChar(Console.ReadLine());
做
{
开关(输入)
{
案例“D”:
存款(参考余额);
打破
案例“P”:
打印(余额);
打破
案例“C”:
支票(参考余额);
打破
违约:
WriteLine(“{0}不是有效的选择”,输入);
打破
}
}while(输入!=“Q”);
}
公共静态无效存款(参考双倍余额)
{
双重存款;
Console.WriteLine(“输入您要存入的金额:”);
存款=Convert.ToInt32(Console.ReadLine());
如果(存款>1000)
{
存款=存款*1.02;
}
其他的
{
存款=存款;
}
余额=+存款;
}
公共静态无效支票(参考双倍余额)
{
var tempBalance=平衡;
双重扣除;
const双倍费用=1;
做
{
控制台。WriteLine(“输入支票金额:”);
演绎=Convert.ToInt32(Console.ReadLine());
}扣除额<0;
tempBalance=余额-扣减-费用;
如果(温度平衡<0)
{
控制台。写下(“账户中没有足够的钱”);
}
其他的
{
平衡=临时平衡;
}
}
公共静态无效打印(双平衡)
{
WriteLine(“您当前的余额为{0.00}”,余额);
}
}

您正在转换为Int,像这样更改

        do
        {
            Console.WriteLine("Enter the amount of the cheque: ");
            deduction = Convert.ToDouble(Console.ReadLine());
        } 
更改此项:

deposit = Convert.ToInt32(Console.ReadLine());
致:

因为你读的不是int,而是double。使用另一个答案中建议的
TryParse
会更好。此外,如果你需要精确性,也许你应该使用双倍,而不是双倍,但如果精度不太重要,则应该使用双倍。

试试以下方法:

double deposit;
double.TryParse(Console.ReadLine(),out deposit);

好的,您正在将输入转换为整数。整数没有小数。可能您想使用
Convert.ToDouble
作为输入。
double deposit;
double.TryParse(Console.ReadLine(),out deposit);
deposit = Double.Parse(Console.ReadLine());