C#:当结果为0时,不在命令行中显示结果

C#:当结果为0时,不在命令行中显示结果,c#,visual-studio,C#,Visual Studio,我在C#的学校有个任务。我在下面的代码中遇到了这个问题(示例) 结果: 厘米数?:350 米数:3,5 整米数量:3 厘米数?:50 米数:0,5 整米数量:0 如果结果为0表示“整米数量”,我不想在控制台中显示“整米数量:”行 像这样: 厘米数?:50 米数:0,5 仅使用“系统”名称空间如何实现这一点?在不久的将来,您肯定会了解控制结构。 只需检查wholeMeters字段的值,并根据结果进行操作 if(wholeMeters != 0) Console.WriteLi

我在C#的学校有个任务。我在下面的代码中遇到了这个问题(示例)

结果:
  • 厘米数?:350
  • 米数:3,5
  • 整米数量:3

  • 厘米数?:50
  • 米数:0,5
  • 整米数量:0

如果结果为0表示“整米数量”,我不想在控制台中显示“整米数量:”行

像这样:

  • 厘米数?:50
  • 米数:0,5

仅使用“系统”名称空间如何实现这一点?

在不久的将来,您肯定会了解控制结构。 只需检查
wholeMeters
字段的值,并根据结果进行操作

if(wholeMeters != 0)
   Console.WriteLine($"Amount of whole meters: {wholeMeters}");

实际上,这是我的练习,我通过一步一步地重新实现代码找到了结果(花了我一天时间!!!:)

}

  • 时间跨度(秒):34567788
    • 这是:
      • 400天
      • 2小时
      • 9分钟
      • 48秒
  • 时间跨度(秒):34567
    • 这是:
      • 9小时
      • 36分钟
      • 7秒时间跨度(以秒为单位):2345这是:
      • 39分钟
      • 5秒
  • 时间跨度(秒):45
    • 这是:
      • 45秒
  • 时间跨度(秒):二十
    • 请输入一个正整数
我知道我必须使用if语句,但我在代码开始时声明了(double)变量,而不是在每次计算之前


无论如何,谢谢你的帮助

使用
if
如何?问题也可能是,if
x!=0
x>0
if(wholeMeters != 0)
   Console.WriteLine($"Amount of whole meters: {wholeMeters}");
static void Main()
{
    do
    {
        Console.Write("Timespan in seconds?: ");
        int timeInSeconds;

        if (int.TryParse(Console.ReadLine(), out timeInSeconds))
        {
            Console.WriteLine("This is:");

            double amountOfDays = timeInSeconds / 86400;
            if (amountOfDays != 0)
                Console.WriteLine($"- {(int)amountOfDays} days");

            double amountOfHours = timeInSeconds / 3600 - ((int)amountOfDays * 24);
            if (amountOfHours != 0)
                Console.WriteLine($"- {(int)amountOfHours} hours");

            double amountOfMinuts = timeInSeconds / 60 - ((int)amountOfHours * 60) - ((int)amountOfDays * 24 * 60);
            if (amountOfMinuts != 0)
                Console.WriteLine($"- {(int)amountOfMinuts} minuts");

            double amountOfSeconds = timeInSeconds - ((int)amountOfMinuts * 60) - ((int)amountOfHours * 60 * 60) - ((int)amountOfDays * 24 * 60 * 60);
            if (amountOfSeconds != 0)
                Console.WriteLine($"- {(int)amountOfSeconds} seconds");
        }
        else
        {
            Console.WriteLine("Please enter a positive integer!");
        }

    } while (true);
}