首页 > 一段c++代码编译通过了,但是没有输出

一段c++代码编译通过了,但是没有输出

#include <iostream>
using namespace std;

class Student{
public:
    Student(){}
    Student(const int &ag, const int &re): age(ag), result(re){}
    void print()
    {
        cout << age << ' ' << result<< endl;
    }


private:
    int age;
    int result;
};

int main()
{
    Student s[5];
    int i = 0;
    while (i < 5)
    {
        s[i] = Student(i+11, (i+1)*20);
        i++;
    }

    int j = 1;
    while (j/2 != 0 && j < 6)
    {
        s[j-1].print();
        j++;
    }

    return 0;
}

因为你的第二个 while 循环根本进不去:

int j = 1;
while (j/2 != 0 && j < 6) {//...}

好歹你也得让它第一次能进去,j 才能自增嘛。。。// 把 1 改成 2 试试?(int j = 2;)

【热门文章】
【热门文章】