下面是我自己做的作业,题目是一条裤子为120元,T恤35,买裤子和T恤的总价格,如果打折怎么算。其中要求价格用decimal类型,折扣是double类型。因为不怎么会用decimal类型所以整个程序运行下来总感觉哪里出错(运行结果为整数...),同时convert也不怎么会用。希望大家能多多指正程序中的错误部分,(我频繁的用convert,总感觉不对)!!在这先谢谢大家了!!!decimal k = 120m;
decimal T = 35m;
double x=0;
double y=0;
string i,j;
Console.WriteLine(@"本商店裤子的价格是120元/条,请分别输入你所买的裤子和T恤的数量");
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
x = Convert.ToInt32(k)*Convert.ToInt32(x);
y = Convert.ToInt32(T) * Convert.ToInt32(y);
int sum1 =Convert.ToInt32( x) +Convert.ToInt32( y);
Console.WriteLine(" 买裤子需支付{0},买T恤需支付{1},\n共计支付为{2}",x,y,sum1);
Console.WriteLine("是否需要打折,是/S,不需要/B");
Console.ReadKey();
if (Console.ReadLine() == "S")
{
double c = 0;
Console.WriteLine("请输入折扣:");
c = Convert.ToInt32(Console.ReadLine());
int sum2 = Convert.ToInt32(c) * Convert.ToInt32(sum1);
Console.WriteLine("您本次消费共计" + sum2);
}
else
{
Console.WriteLine("您本次消费共计" + sum1);
}
Console.Read();
在这先谢谢大家了!!
decimal T = 35m;
double x=0;
double y=0;
string i,j;
Console.WriteLine(@"本商店裤子的价格是120元/条,请分别输入你所买的裤子和T恤的数量");
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
x = Convert.ToInt32(k)*Convert.ToInt32(x);
y = Convert.ToInt32(T) * Convert.ToInt32(y);
int sum1 =Convert.ToInt32( x) +Convert.ToInt32( y);
Console.WriteLine(" 买裤子需支付{0},买T恤需支付{1},\n共计支付为{2}",x,y,sum1);
Console.WriteLine("是否需要打折,是/S,不需要/B");
Console.ReadKey();
if (Console.ReadLine() == "S")
{
double c = 0;
Console.WriteLine("请输入折扣:");
c = Convert.ToInt32(Console.ReadLine());
int sum2 = Convert.ToInt32(c) * Convert.ToInt32(sum1);
Console.WriteLine("您本次消费共计" + sum2);
}
else
{
Console.WriteLine("您本次消费共计" + sum1);
}
Console.Read();
在这先谢谢大家了!!
y = Convert.ToInt32(Console.ReadLine());这边有问题,应该是Convert.ToDouble
我将数据改成double进行重新运行了下,但是结果还是整型,
应该是这句话出错 int sum2 = Convert.ToInt32(c) * Convert.ToInt32(sum1);,但是不知道怎么改...
y = Convert.ToInt32(T) * Convert.ToInt32(y);
int sum1 =Convert.ToInt32( x) +Convert.ToInt32( y);
楼主前两行最后的x,y不需要再转换为int类型了,
第三行也一样