我在使用此代码时遇到问题。每当我输入249或更低的值时,它就可以正常工作。但是其他任何东西都让我得到了else
声明:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ILAB02
{
class Program
{
static void Main(string[] args)
{
double salesAmount, shippingAmount;
salesAmount = 0.00;
shippingAmount = 0.00;
Console.WriteLine("What is the total amount of sales?");
salesAmount = Convert.ToDouble(Console.ReadLine());
if (salesAmount > 5000.00)
{
shippingAmount = 20.00;
}
if (salesAmount > 1000.00 && salesAmount <= 5000.00)
{
shippingAmount = 15.00;
}
if (salesAmount > 500.00 && salesAmount<=1000.00)
{
shippingAmount = 10.00;
}
if (salesAmount > 250.00 && salesAmount <=500.00)
{
shippingAmount = 8.00;
}
if (salesAmount > 0.00 && salesAmount <=250.00)
{
shippingAmount = 5.00;
}
else
{
shippingAmount = 0.00;
Console.WriteLine("Error incorrect input!");
}
Console.WriteLine("Total sales amount is {0:C}",salesAmount);
Console.WriteLine("Shipping charges is {0:C}", shippingAmount);
Console.ReadLine();
}
}
}
答案 0 :(得分:3)
您需要使用else if
。现在你的代码将检查第一个if
,如果不是这样,它会直接进入else
,因为它应该做,而其他条件永远不会被检查:
if (salesAmount > 5000.00)
{
shippingAmount = 20.00;
}
else if (salesAmount > 1000.00 && salesAmount <= 5000.00)
{
shippingAmount = 15.00;
}
else if (salesAmount > 500.00 && salesAmount<=1000.00)
{
shippingAmount = 10.00;
}
else if (salesAmount > 250.00 && salesAmount <=500.00)
{
shippingAmount = 8.00;
}
else if (salesAmount > 0.00 && salesAmount <=250.00)
{
shippingAmount = 5.00;
}
else
{
shippingAmount = 0.00;
Console.WriteLine("Error incorrect input!");
}
编辑:小侧边节点,你的标题应该与你的问题有关。例如“if-condition not reach”。
答案 1 :(得分:0)
这个简单的我的朋友,只需将所有&#39;除了第一个以外的所有人改为&#39; ElseIf&#39;