小编Dan*_*idt的帖子

C ++程序,将两个整数之间的数字相加

这是我的代码,当x <y时有效。但是当x> y时不是。我怎样才能解决这个问题?我知道问题在while循环内,并且我尝试了很多不同的方法,但无法解决问题。

int main()
{
    int x,y;

    int total = 0;
    cout<<"Please give me an integer: ";
    cin >> x;

    cout<<"Please give me another integer: ";
    cin >> y;

    int counter = x;
    while(counter <= y ){

        total += counter;
        ++counter;    
    }

    cout << "The total of the numbers " << total<<endl;      
}
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
73
查看次数

标签 统计

c++ ×1