我有这个问题,我必须为学校的任务做。我有麻烦使它与小数一起工作。如果有人可以帮助我让程序接受小数。它将输出一个小数但它不会让我输入一个。
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);
}
}
答案 0 :(得分:0)
您正在转换为Int,更改为
do
{
Console.WriteLine("Enter the amount of the cheque: ");
deduction = Convert.ToDouble(Console.ReadLine());
}
答案 1 :(得分:0)
改变这个:
deposit = Convert.ToInt32(Console.ReadLine());
为:
deposit = Convert.ToDouble(Console.ReadLine());
因为你不是在读一个int,而是一个double。使用另一个答案中建议的TryParse
会更好。另外,也许你应该使用decimal,如果你需要准确的话,不要加倍,但如果精确度不那么重要,那就加倍。
答案 2 :(得分:0)
试试这个:
double deposit;
double.TryParse(Console.ReadLine(),out deposit);
答案 3 :(得分:0)
deposit = Double.Parse(Console.ReadLine());