【问题标题】:HackerRank:- Day 4: Class vs. Instance, some weird error for some reasonHackerRank:- 第 4 天:类与实例,出于某种原因出现一些奇怪的错误
【发布时间】:2020-03-03 18:09:34
【问题描述】:

此测试出错:- https://www.hackerrank.com/challenges/30-class-vs-instance/problem?h_r=next-challenge&h_v=zen 一个测试用例由于某种原因失败了,当我在我的 IDE 中运行它时,它给出了与预期相同的输出,帮助我找出我错过了什么,我是非常初学者级别的编码器,从 Civil Engg 背景移动,所以如果出现错误,我很抱歉真的很傻。我的代码:

#include <iostream>
using namespace std;

class Person{
    public:
        int age;
        Person(int initialAge);
        void amIOld();
        void yearPasses();
    };

    Person::Person(int initialAge){
        if (initialAge < 0){
            this->age=0;
            cout << "Age is not valid, setting age to 0.";
        }
        else {
            this->age = initialAge;
        }

    }

    void Person::amIOld(){
        if(age < 13){
            cout << "\nYou are young.";
        }
        else if (age >= 13 && age < 18) {
            cout << "\nYou are a teenager.";
        }
        else {
            cout << "\nYou are old.";
        }
    }

    void Person::yearPasses(){
        age++;
    }

int main(){
    int t;
    int age;
    cin >> t;
    for(int i=0; i < t; i++) {
        cin >> age;
        Person p(age);
        p.amIOld();
        for(int j=0; j < 3; j++) {
            p.yearPasses(); 
        }
        p.amIOld();

        cout << '\n';
    }

    return 0;
}

【问题讨论】:

  • 顺便说一句,您可以通过不使用this-&gt; 来节省一些打字时间。直接访问变量:例如age = initialAge; 构造函数不需要this-&gt; 表示法,并且您不要在其他方法中使用该表示法。
  • 也许是你的换行处理。在年龄小于 0 的 ctor 中,您不会打印换行符。但是在amIOld 中,您在文本之前打印换行符。
  • 请注意,else if 测试中可以省略age &gt;= 13 &amp;&amp; 部分!
  • 明白了!摆脱了那个年龄 >= 13 的条件,因为它没有用

标签: c++ c++14


【解决方案1】:

这是你放在开头而不是结尾的新行结尾,

cout << "\nYou are old.";

把它们放在最后应该可以解决它。这些测试检查准确的输出,很多时候答案似乎正确,但轻微的输出差异会使测试用例失败。

为什么失败的解释:通过将行尾放在开头,您假设已经有一个输出/行。因此,在初始年龄不

【讨论】:

  • 谢谢,最后用 endl 代替它修复了它!
【解决方案2】:

您还需要将 this->age 放在第二个函数上

void Person::amIOld(){
    if(this->age < 13){ //you need to put this->age all age as well
        cout << "\nYou are young.";
    }
    else if (this->age >= 13 && this->age < 18) {//you need to put this->age all age as well
        cout << "\nYou are a teenager.";
    }
    else {
        cout << "\nYou are old.";
    }
}

您还需要将 this->age 也放在最后一个函数中

void Person::yearPasses(){
    this->age++; //you need to put this->age all age as well
}

【讨论】:

    【解决方案3】:

    #python 3

    class Person:
        def __init__(self,initialAge):
            if initialAge < 3:
                print('Age is not valid, setting age to 0.')
                self.age = 0
            else:
                self.age = initialAge
            # Add some more code to run some checks on initialAge
        def amIOld(self):
            if self.age < 13:
                print("You are young.")
            elif self.age < 18:
                print("You are a teenager.")
            else:
                print("You are old. " , ) 
            # Do some computations in here and print out the correct statement to the console
        def yearPasses(self):
            self.age += 1
            # Increment the age of the person in here
    
    
    
    
    t = int(input())
    for i in range(0, t):
        age = int(input())         
        p = Person(age)  
        p.amIOld()
        for j in range(0, 3):
            p.yearPasses()       
        p.amIOld()
        print("")
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2013-01-15
    • 2014-11-22
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多