Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用<;时获得太多输出<;=>;=操作员_C# - Fatal编程技术网

C# 使用<;时获得太多输出<;=>;=操作员

C# 使用<;时获得太多输出<;=>;=操作员,c#,C#,我一直在为一家鱼类日托所做一个项目。这种想法是,客户输入他们的信息(名称、鱼类名称、鱼类种类和鱼的长度,以英寸为单位),并根据他们以英寸为单位输入的内容,在最后返回他们的信息和费用。我把它都写出来了,它做了我想做的事情,除了它得到的费用超过了它应该得到的 例如:如果用户将鱼的长度输入为21,而不是输出“Fee:16$”,它将同时输出“Fee:12$”和“Fee:16$” 我知道这与我如何设置运算符(,=)有关,这就是为什么它的输出比我想要的要多,我就是不知道如何修复它 代码如下: namespa

我一直在为一家鱼类日托所做一个项目。这种想法是,客户输入他们的信息(名称、鱼类名称、鱼类种类和鱼的长度,以英寸为单位),并根据他们以英寸为单位输入的内容,在最后返回他们的信息和费用。我把它都写出来了,它做了我想做的事情,除了它得到的费用超过了它应该得到的

例如:如果用户将鱼的长度输入为21,而不是输出“Fee:16$”,它将同时输出“Fee:12$”和“Fee:16$”

我知道这与我如何设置运算符(,=)有关,这就是为什么它的输出比我想要的要多,我就是不知道如何修复它

代码如下:

namespace Fish_O_Rama
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declaring constants for fish sizes
            const int FishSizeSmall = 9;
            const int FishSizeMed1 = 10;
            const int FishSizeMed2 = 20;
            const int FishSizeBig1 = 21;
            const int FishSizeBig2 = 30;
            const int FishSizeBiggest = 31;

            //Declaring constants for the fees
            const string FishMonSmall = ("Fee: 7$");
            const string FishMonMed = ("Fee: 12$");
            const string FishMonBig = ("Fee: 16$");
            const string FishMonBiggest = ("Fee: 20$");

            //Accepting input from the customer
            Console.Write("Please enter your name: ");
            string CustName = Console.ReadLine();
            Console.Write("Please enter your fishes name: ");
            string FishName = Console.ReadLine();
            Console.Write("Please enter your fishes species: ");
            string FishSpecies = Console.ReadLine();
            Console.Write("Please enter the length of your fish in inches: ");
            string FishLength = Console.ReadLine();

            //Outputting the users input
            Console.WriteLine("--------------------------");
            Console.WriteLine("Name: " + CustName);
            Console.WriteLine("Fishes Name: " + FishName);
            Console.WriteLine("Species: " + FishSpecies);

            //Fish under 10 inches
            if (Convert.ToInt32(FishLength) < FishSizeSmall)
            {
                Console.WriteLine();
                Console.WriteLine(FishMonSmall); 
            }

            //Fish 10 inches to 20 inches
            if (Convert.ToInt32(FishLength) >= FishSizeMed1 || Convert.ToInt32(FishLength) <= FishSizeMed2)
            {
                Console.WriteLine();
                Console.WriteLine(FishMonMed);
            }

            //Fish greater than 20 inches but up to 30 inches
            if (Convert.ToInt32(FishLength) >= FishSizeBig1 || Convert.ToInt32(FishLength) <= FishSizeBig2)
            {
                Console.WriteLine();
                Console.WriteLine(FishMonBig);
            }

            //Fish greater than 30 inches
            if (Convert.ToInt32(FishLength) > FishSizeBiggest)
            {
                Console.WriteLine();
                Console.WriteLine(FishMonBiggest);
            }

        }
    }
}
namespace Fish\u O\u Rama
{
班级计划
{
静态void Main(字符串[]参数)
{
//声明鱼类大小的常量
常数int FishSizeSmall=9;
常数int FishSizeMed1=10;
常数int FishSizeMed2=20;
常数int FishSizeBig1=21;
常数int FishSizeBig2=30;
常数int FishSizeBiggest=31;
//声明费用的常量
常量字符串FishMonSmall=(“费用:7美元”);
常量字符串FishMonMed=(“费用:12美元”);
常量字符串FishMonBig=(“费用:16美元”);
const string fishmonmax=(“费用:20美元”);
//接受客户的输入
控制台。写下(“请输入您的姓名:”);
字符串CustName=Console.ReadLine();
控制台。写下(“请输入您的名字:”);
字符串FishName=Console.ReadLine();
控制台。写下(“请输入您的鱼类种类:”;
字符串FishSpecies=Console.ReadLine();
控制台。写下(“请以英寸为单位输入鱼的长度:”;
字符串FishLength=Console.ReadLine();
//输出用户输入
Console.WriteLine(“-----------------------------------”);
Console.WriteLine(“名称:”+CustName);
控制台.WriteLine(“鱼名:”+FishName);
控制台。WriteLine(“种类:+鱼类种类”);
//10英寸以下的鱼
if(转换为32(FishLength)=FishSizeMed1 | | Convert.ToInt32(FishLength)=FishSizeBig1 | | Convert.ToInt32(FishLength)FishSizeBigTest)
{
Console.WriteLine();
控制台写入线(Fishmon最大);
}
}
}
}

如果有任何帮助,我们将不胜感激。

事实证明我使用的是逻辑or语句“| |”wen我应该使用逻辑and语句“&&”

像这样:

//Fish 10 inches to 20 inches
        if (Convert.ToInt32(FishLength) >= FishSizeMed1 && Convert.ToInt32(FishLength) <= FishSizeMed2)
        {
            Console.WriteLine();
            Console.WriteLine(FishMonMed);
        }
//钓10英寸到20英寸

如果(Convert.ToInt32(FishLength)>=FishSizeMed1&&Convert.ToInt32(FishLength)因为鱼只能有一个长度,所以使用“if/else if”语句而不是一系列“if”语句可能有意义。这样,当“if/else if”中的条件计算结果为
true
,而不是在确定正确的案例后可能测试其他不必要的案例。如果长度为21

if (Convert.ToInt32(FishLength) >= FishSizeMed1 || Convert.ToInt32(FishLength) <= FishSizeMed2)
由于与上述相同的原因,计算结果也为
true
:10大于或等于21,即
FishSizeBig1
的值


理解两个AND和OR布尔表达式之间的差异将解决此问题,并使用“if/else if”链接语句将使应用程序更加高效。

否则
也可能是,
&
而不是
|
?学习如何使用将是非常宝贵的。@DourHighArch:我使用的是Visual Studio,它是否默认启用了所有编译器警告?我是新手,如果我问了一些明显的问题,我向您道歉。您可以看到什么com皮尔警告你已经打开了。我建议你读一本。@DourHighArch会好好读一读的。谢谢你,伙计。
if (Convert.ToInt32(FishLength) >= FishSizeBig1 || Convert.ToInt32(FishLength) <= FishSizeBig2)