【发布时间】: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后的分号