【问题标题】:I can't get any output and my statements don't execute properly because of this, however, every if statement does execute even when it shouldn't? [closed]我无法获得任何输出,并且我的语句因此无法正确执行,但是,每个 if 语句都会执行,即使它不应该执行? [关闭]
【发布时间】:2021-02-16 12:44:08
【问题描述】:

我想编写一个程序,将四个数字相乘并输出它们的符号,无论答案是负数还是正数。我必须使用逻辑运算符编写程序,但问题是我想先尝试使用简单的 if 语句系列,同时还要使用关系运算符。我编写了这段代码,并多次检查,代码编译并执行,但没有显示输出。它说“答案是”,后跟一个空格,并显示了我的所有 3 个 if 语句。有人可以请为我修复我的代码(并可能告诉我如何使用逻辑运算符编写这样的程序,其中我给出 4 个输入,我得到的输出说明将 4 个数字相乘后的答案是正数还是负数,如果是有什么方法可以做到这一点,而无需实际计算也就是乘法)并告诉我我的代码到底出了什么问题。代码在这里给出:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program746
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, d;
            Console.WriteLine("Please enter the first number:");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the second number:");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the third number:");
            c = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the fourth number:");
            d = Convert.ToInt32(Console.ReadLine());

            int e = a * b * c * d; 
            if (e < 0) ;
            {
                Console.WriteLine("The result is negative and has the '-' sign ");
            }

            if (e > 0) ;
            {
                Console.WriteLine("The result is positive and has the'+' sign");
            }

            if (e == 0) ;
            {
                Console.WriteLine("The result is zero, and does not have a sign");
            }
            Console.WriteLine("The answer is ", e);
            Console.ReadKey();
        }
    }
} 

【问题讨论】:

  • VTC 是错字。去掉ifs后的分号

标签: c# c#-4.0


【解决方案1】:
static void Main(string[] args)
        {
            int a, b, c, d;
            Console.WriteLine("Please enter the first number:");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the second number:");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the third number:");
            c = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter the fourth number:");
            d = Convert.ToInt32(Console.ReadLine());

            int e = a * b * c * d;
            if (e < 0)
            {
                Console.WriteLine("The result is negative and has the '-' sign ");
            }

            if (e > 0)
            {
                Console.WriteLine("The result is positive and has the'+' sign");
            }

            if (e == 0)
            {
                Console.WriteLine("The result is zero, and does not have a sign");
            }
            Console.WriteLine("The answer is {0}", e);
            Console.ReadKey();
        }

【讨论】:

  • 运行这段代码,一切都会好起来的!
  • Furqan Sab,谢谢你的帮助,但是我想问一下是否有任何方法可以编写一个程序,它需要 4 个输入并在不实际乘以它们的情况下判断它们的乘积是正数还是负数,我是告诉它可以使用逻辑运算符来完成,但我似乎无法弄清楚如何:/
【解决方案2】:

删除每个 if 语句后的分号。因为最后用分号终止 if。 您可以查看此链接:https://social.msdn.microsoft.com/Forums/en-US/3508c3a2-fd1c-49db-93ea-f7bb5c2d27f4/extra-semicolon-suberst-conditional-flow?forum=csharpgeneral

你的如果:

if (e < 0) ;
{
    ...
}

如果正确:

if (e < 0)
{
    ...
}

您可以使用 i = 4 进行 for 循环,并将每个数字乘以结果 int 或将它们保存在 int 数组中,而不是使用四个 ReadLine。

我会这样写程序:

static void Main(string[] args)
    {
        int result = PrintSign(4);

        Console.WriteLine(string.Format("The answer is {0}", result));
        Console.ReadLine();
    }

    private static int PrintSign(int maxNumbers)
    {
        int result = 0;

        for (int i = 0; i < maxNumbers; i++)
        {
            int number = 0;
            while (true)
            {
                Console.WriteLine(string.Format("Please enter number {0}:", i));
                if (int.TryParse(Console.ReadLine(), out number))
                    break;
            }

            // if one number is zero, return 0 (multiply with zero and it's zero all the time)
            if (number == 0)
                return 0;
            result *= number;
        }

        // check for negative number
        if (result < 0)
            Console.WriteLine("The result is negative and has the '-' sign");
        // else, because zero is not possible at this point
        else
            Console.WriteLine("The result is positive and has the'+' sign");

        return result;
    }

如果这回答了您的问题,请点赞并接受答案:)

【讨论】:

  • OP 还应注意 the compiler tried to warn them about this - “可能错误的空语句。条件语句后的分号可能会导致您的代码执行方式与预期不同。”
  • 谢谢先生,它确实帮助解决了我的问题,但我的另一个问题是是否还有另一种方法可以使用逻辑运算符编写相同的程序,该程序不应该计算 4 个数字来判断一个数字是正面的还是负面的?帮助将不胜感激:)
猜你喜欢
  • 2019-07-29
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 2016-03-28
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多