我该如何使我的程序输出 [英] How can i make my program output this

查看:105
本文介绍了我该如何使我的程序输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为此创建三个布尔变量,但是我对如何做到这一点感到困惑.

示例输出1(不是a年,输入以粗体斜体显示):
输入年份: 1800
年份是否可以被4整除?是的.
年份是否可以被100整除?是的.
年份是否可以被400整除?否
1800年不是a年.


示例输出2(是a年,输入以粗体斜体显示):
输入年份: 2364
年份是否可以被4整除?是的.
年份是否可以被100整除?否
年份是否可以被400整除?否
2104是a年.

我尝试过的事情:

到目前为止,这就是我所拥有的.但是我不觉得自己走在正确的轨道上.

I have to create three Boolean variables for this but I''m confused on how to do so.

Example Output 1 (not a leap year, input is in bolded italics):
Enter a year: 1800
Is the year evenly divisible by 4? Yes.
Is the year evenly divisible by 100? Yes.
Is the year evenly divisible by 400? No.
1800 is not a leap year.


Example Output 2 (is a leap year, input is in bolded italics):
Enter a year: 2364
Is the year evenly divisible by 4? Yes.
Is the year evenly divisible by 100? No.
Is the year evenly divisible by 400? No.
2104 is a leap year.

What I have tried:

This is what I have so far. But I don''t feel like I''m on the right track.

 #include <iostream>
#include <string>
using namespace std;

int main ()
{
    int year;

    cout << "Enter a year: ";
    cin >> year;
    cout << endl;
    
    if (leap_year(year) == true)
    {
        cout << "Is the year evenly divisible by 4? Yes." 
    }
    if (leap_year(year) == false)
    {
        cout << "Is the year evenly"
    }
        
return 0;
}</string></iostream>

推荐答案

编写您的函数jump_year,以便它返回bool值.
在函数内部,进行第一个测试.如果失败,则返回false.
做第二个测试.如果失败,则返回false.
做最后的测试.如果失败,则返回false.
否则,返回true.
在您的主要功能中,将代码更改为:
Write your function leap_year so that it returns a bool value.
Inside the function, do the first test. If it fails, return false.
Do the second test. If it fails, return false.
Do the final test. If it fails, return false.
Otherwise, return true.
In your main function, change the code to:
if (leap_year(year))
{
    cout << year << " is a leap year.";
}
else
{
    cout << year << "Is not a leap year."
}


您必须在jump_year函数中添加一些逻辑.我已经阅读了维基百科.可能是对的

You must put some logic in your leap_year function. I have read the wikipedia for it. May it is right

bool leap_year(int year)
{
//never a leap year
 if( (year % 4) != 0) return false;

  if( (year % 100) != 0) return true;
  //the big case
  if( (year % 400) == 0) return false;

  return true;
}



提示:编写测试功能,如



tip: write test functions like

ASSERT( leap_year(1980) );//is leap year
ASSERT( !leap_year(1981) );//not leap year


您可以尝试检查year年是否为leap年.

You can try this to check year is leap year or not.

int year;
if((year % 400 == 0) || ((year % 4 == 0)&&(year % 100 != 0)))
    return true;  // year is leap year
else
    return false;  // not a leap year




leap年的条件.
*如果年份完全除以400.

*如果year可以被4整除而不是100.




Condition for leap year.
*if year is completely divided by 400.
OR
*if year is completely divisible by 4 but not by 100.


这篇关于我该如何使我的程序输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆