N
N
NikitaSova2020-06-13 11:22:54
C++ / C#
NikitaSova, 2020-06-13 11:22:54

How to solve the problem with classes?

I recently wrote code that writes information about objects (in this case, about cats). There are no errors, but in the end it gives out some kind of beleberd. I tried to solve this problem but could not find the root of this problem, so I decided to contact you.
It outputs this:

Имя:  возраст: 0 пушистость:  цвет:  вес: 0кг
Имя:  возраст: 0 пушистость:  цвет:  вес: 0кг
Имя:  возраст: 0 пушистость:  цвет:  вес: 0кг
Имя:  возраст: 0 пушистость:  цвет:  вес: 0кг
Имя: Пшик возраст: 3 пушистость: пушистый цвет: серый вес: 5кг
Имя:  возраст: 0 пушистость:  цвет:  вес: 5кг

And you need him to write two lines that will contain information about cats.
Here is the code:
using System;
using System.Numerics;

namespace HelloApp1
{
    class Cat
    {
        public string name;
        public int age;
        public string hair;
        public string color;
        public int weight;
        public string name1;
        public int age1;
        public string hair1;
        public string color1;
        public int weight1;

        public void GetInfo()
        {
            Console.WriteLine($"Имя: {name} возраст: {age} пушистость: {hair} цвет: {color} вес: {weight}кг");
            Console.WriteLine($"Имя: {name1} возраст: {age1} пушистость: {hair1} цвет: {color1} вес: {weight}кг");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cat Pshik = new Cat();
            Pshik.GetInfo();
            Cat Kote = new Cat();
            Kote.GetInfo();

            Pshik.name = "Пшик";
            Pshik.age = 3;
            Pshik.hair = "пушистый";
            Pshik.color = "серый";
            Pshik.weight = 5;
            Pshik.GetInfo();
            Console.ReadKey();

            Kote.name1 = "Котэ";
            Kote.age1 = 9;
            Kote.hair1 = "гладкошёрстный";
            Kote.color1 = "чёрно-белый";
            Kote.weight1 = 5;
            Kote.GetInfo();
            Console.ReadKey();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-06-13
@NikitaSova

and if so?
https://ideone.com/4MqdOY
ps updated (for general knowledge)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question