简体   繁体   English

我的程序有问题吗? 我似乎只得到 0.0000 作为我的答案

[英]Is there something wrong with my program? I only seems to get 0.0000 as my answers

#include <stdio.h>
void main (void)    
{
    int days, seconds;
    
    printf("Enter the no. of days: ");
    scanf("%d", &days);
    
    seconds = days*36400;
    
    if (days<0)
    {
        printf("Invalid input!");
    }
    else 
    {
        printf("It is equal to %.5f seconds", seconds);
    }
}

You declared seconds as an integer, so you should change %.5f to %d .您将 seconds 声明为 integer,因此您应该将%.5f更改为%d

#include <stdio.h>
void main (void)
{
  int days, seconds;

  printf("Enter the no. of days: ");
  scanf("%d", &days);
  
  seconds = days*36400;
 
  if (days<0)
    {
      printf("Invalid input!");
    }
  else 
    {
      printf("It is equal to %d seconds", seconds);
    }
  
}

There are couple of mistakes in your code您的代码中有几个错误

  1. Before converting days into second you need to check for valid and invalid input.在将天数转换为秒数之前,您需要检查有效和无效输入。
  2. You have taken variables in int and printing them as floating value.您已经在 int 中获取变量并将它们打印为浮点值。

Yo can use following code to get the output you want您可以使用以下代码获取您想要的 output

#include <stdio.h>

int main(void) {
    int days, seconds;

    printf("Enter the no. of days: ");
    scanf("%d", &days);
    if(days<=0){
        printf("Invalid input");
    }else{
        seconds = days * 86400;
        printf("It is equal to: %.5d Seconds", seconds);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的字符排序程序会产生错误的答案? - Why does my character sorting program produce wrong answers? 我应该如何处理返回错误答案的函数? - What should I do with my functions that return wrong answers? 我的背包有什么问题吗 - Is there something wrong with my knapsack 打印 ASCII 代码字符的程序有问题吗? - Is there something wrong in my program that prints the characters for the ASCII CODE? 你能想到一个可以让我的程序给出错误答案的测试吗? - Can you think of a test which could make my program give wrong answers? 有人请告诉我为什么我的代码给了我错误的答案。程序应该接受一个整数输入并给出相应的阶乘。我 - Someone please tell me why my code gives me wrong answers.The program is supposed to accept an integer input and give the corresponding factorial.I 我的返回值有问题吗? - Is something wrong with my return value? 我的日期代码有问题吗? - Is there something wrong with my date code? 在我调用函数扫描某些东西后,我的程序没有继续? 有人能发现什么是错的吗? (很长,但请帮助!) - My program does not continue after I call a function to scan something? Can someone find out whats wrong? (long but please help!) 我的服务器程序仅在第二个请求到达时才回答第一个请求? - My server program only answers the first request when a second one arrives?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM