Софтуерно Инженерство
Loading...
+ Нов въпрос
markovood avatar markovood 8 Точки

06. Vehicle Catalogue от Objects and Classes - Exercise

Хора пращам си решението на тази задача и получавам много странна грешка в judge: "

The process executing your submission for this test may not have received the output successfully. Please try to submit again the same solution. If the result does not change, then search the error in the submission itself.

"

Ето го и кодът ми ако някой може да помогне моля: 

using System;
using System.Collections.Generic;
using System.Linq;

namespace _06._Vehicle_Catalogue
{
    public class Vehicle_Catalogue
    {
        public static void Main()
        {
            List<Vehicle> catalogue = new List<Vehicle>();

            while (true)
            {
                string[] inputTokens = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                if (inputTokens[0] == "End")
                {
                    break;
                }

                string type = inputTokens[0].ToLower();
                string model = inputTokens[1];
                string color = inputTokens[2].ToLower();
                int horsePower = int.Parse(inputTokens[3]);
                var currentVehicle = new Vehicle(type, model, color, horsePower);
                catalogue.Add(currentVehicle);
            }

            while (true)
            {
                string model = Console.ReadLine();
                if (model == "Close the Catalogue")
                {
                    break;
                }

                Console.WriteLine(catalogue.Find(x => x.Model == model));
            }

            var onlyCars = catalogue.Where(x => x.Type == "car").ToList();
            var onlyTrucks = catalogue.Where(x => x.Type == "truck").ToList();
            double totalCarsHorsepower = 0;
            double totalTrucksHorsepower = 0;

            foreach (var car in onlyCars)
            {
                totalCarsHorsepower += car.HorsePower;
            }

            foreach (var truck in onlyTrucks)
            {
                totalTrucksHorsepower += truck.HorsePower;
            }

            double averageCarsHorsepower = totalCarsHorsepower / onlyCars.Count;
            double averageTrucksHorsepower = totalTrucksHorsepower / onlyTrucks.Count;
            if (averageCarsHorsepower > 0 && averageTrucksHorsepower > 0)
            {
                Console.WriteLine($"Cars have average horsepower of: {averageCarsHorsepower:f2}.");
                Console.WriteLine($"Trucks have average horsepower of: {averageTrucksHorsepower:f2}.");
            }
            else if (averageCarsHorsepower > 0 && averageTrucksHorsepower <= 0)
            {
                Console.WriteLine($"Cars have average horsepower of: {averageCarsHorsepower:f2}.");
            }
            else if (averageCarsHorsepower <= 0 && averageTrucksHorsepower > 0)
            {
                Console.WriteLine($"Trucks have average horsepower of: {averageTrucksHorsepower:f2}.");
            }
        }
    }

    public class Vehicle
    {
        public Vehicle(string type, string model, string color, int horsePower)
        {
            this.Type = type;
            this.Model = model;
            this.Color = color;
            this.HorsePower = horsePower;
        }

        public string Type { get; set; }
        public string Model { get; set; }
        public string Color { get; set; }
        public int HorsePower { get; set; }

        public override string ToString()
        {
            string vehicleStr = $"Type: {(this.Type == "car" ? "Car" : "Truck")}{Environment.NewLine}" +
                                $"Model: {this.Model}{Environment.NewLine}" +
                                $"Color: {this.Color}{Environment.NewLine}" +
                                $"Horsepower: {this.HorsePower}";

            return vehicleStr;
        }
    }
}

 

Тагове:
1
Fundamentals Module
VasilKostov avatar VasilKostov 134 Точки
Best Answer
using System;
using System.Collections.Generic;
using System.Linq;

namespace _06._Vehicle_Catalogue
{
    public class Vehicle_Catalogue
    {
        public static void Main()
        {
            List<Vehicle> catalogue = new List<Vehicle>();

            while (true)
            {
                string[] inputTokens = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                if (inputTokens[0] == "End")
                {
                    break;
                }

                string type = inputTokens[0].ToLower();
                string model = inputTokens[1];
                string color = inputTokens[2].ToLower();
                int horsePower = int.Parse(inputTokens[3]);
                var currentVehicle = new Vehicle(type, model, color, horsePower);
                catalogue.Add(currentVehicle);
            }

            while (true)
            {
                string model = Console.ReadLine();
                if (model == "Close the Catalogue")
                {
                    break;
                }

                Console.WriteLine(catalogue.Find(x => x.Model == model));
            }

            var onlyCars = catalogue.Where(x => x.Type == "car").ToList();
            var onlyTrucks = catalogue.Where(x => x.Type == "truck").ToList();
            double totalCarsHorsepower = 0;
            double totalTrucksHorsepower = 0;

            foreach (var car in onlyCars)
            {
                totalCarsHorsepower += car.HorsePower;
            }

            foreach (var truck in onlyTrucks)
            {
                totalTrucksHorsepower += truck.HorsePower;
            }

            double averageCarsHorsepower = totalCarsHorsepower / onlyCars.Count;
            double averageTrucksHorsepower = totalTrucksHorsepower / onlyTrucks.Count;

            if (onlyCars.Count > 0)
            {
                Console.WriteLine($"Cars have average horsepower of: {averageCarsHorsepower:f2}.");
                
            }
            else
            {
                Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
            }

            if (onlyTrucks.Count > 0)
            {
                Console.WriteLine($"Trucks have average horsepower of: {averageTrucksHorsepower:f2}.");
            }
            else
            {
                Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
            }
        }
    }

    public class Vehicle
    {
        public Vehicle(string type, string model, string color, int horsePower)
        {
            this.Type = type;
            this.Model = model;
            this.Color = color;
            this.HorsePower = horsePower;
        }

        public string Type { get; set; }
        public string Model { get; set; }
        public string Color { get; set; }
        public int HorsePower { get; set; }

        public override string ToString()
        {
            string vehicleStr = $"Type: {(this.Type == "car" ? "Car" : "Truck")}{Environment.NewLine}" +
                                $"Model: {this.Model}{Environment.NewLine}" +
                                $"Color: {this.Color}{Environment.NewLine}" +
                                $"Horsepower: {this.HorsePower}";

            return vehicleStr;
        }
    }
}

 

0
VasilKostov avatar VasilKostov 134 Точки

Еото ти кода, който минава 100/100. Само това промених:

if (onlyCars.Count > 0)
{
    Console.WriteLine($"Cars have average horsepower of: {averageCarsHorsepower:f2}.");
                
}
else
{
    Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
}

if (onlyTrucks.Count > 0)
{
    Console.WriteLine($"Trucks have average horsepower of: {averageTrucksHorsepower:f2}.");
}
else
{
    Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
}

 

0
10/11/2018 13:50:11
VasilKostov avatar VasilKostov 134 Точки

Привет,

ако имаш само един тип Vehicles - за другия вид, също трябва да отпечаташ резултат за averageHorsePower. Следователно, виинаги си принтираш и за колите, и за камионите, а когато едните липсват - averageHP за тях е 0.00

0
markovood avatar markovood 8 Точки

Явно не е това само проблема щото все още получавам само 33/100

0
Iliyan_ts avatar Iliyan_ts 5 Точки

Колега, ако успееш да я разгадаеш, ще те помоля да споделиш решението, защото аз я мъча от 2 дена и не мърда от 33/100

0
markovood avatar markovood 8 Точки
using System;
using System.Collections.Generic;
using System.Linq;

namespace _06._Vehicle_Catalogue
{
    public class Vehicle_Catalogue
    {
        public static void Main()
        {
            List<Vehicle> catalogue = new List<Vehicle>();

            while (true)
            {
                string[] inputTokens = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                if (inputTokens[0] == "End")
                {
                    break;
                }

                string type = inputTokens[0].ToLower();
                string model = inputTokens[1];
                string color = inputTokens[2].ToLower();
                int horsePower = int.Parse(inputTokens[3]);
                var currentVehicle = new Vehicle(type, model, color, horsePower);
                catalogue.Add(currentVehicle);
            }

            while (true)
            {
                string model = Console.ReadLine();
                if (model == "Close the Catalogue")
                {
                    break;
                }

                Console.WriteLine(catalogue.Find(x => x.Model == model));
            }

            var onlyCars = catalogue.Where(x => x.Type == "car").ToList();
            var onlyTrucks = catalogue.Where(x => x.Type == "truck").ToList();
            double totalCarsHorsepower = 0;
            double totalTrucksHorsepower = 0;

            foreach (var car in onlyCars)
            {
                totalCarsHorsepower += car.HorsePower;
            }

            foreach (var truck in onlyTrucks)
            {
                totalTrucksHorsepower += truck.HorsePower;
            }

            double averageCarsHorsepower = totalCarsHorsepower / onlyCars.Count;
            double averageTrucksHorsepower = totalTrucksHorsepower / onlyTrucks.Count;

            if (onlyCars.Count > 0)
            {
                Console.WriteLine($"Cars have average horsepower of: {averageCarsHorsepower:f2}.");
                
            }
            else
            {
                Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
            }

            if (onlyTrucks.Count > 0)
            {
                Console.WriteLine($"Trucks have average horsepower of: {averageTrucksHorsepower:f2}.");
            }
            else
            {
                Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
            }
        }
    }

    public class Vehicle
    {
        public Vehicle(string type, string model, string color, int horsePower)
        {
            this.Type = type;
            this.Model = model;
            this.Color = color;
            this.HorsePower = horsePower;
        }

        public string Type { get; set; }
        public string Model { get; set; }
        public string Color { get; set; }
        public int HorsePower { get; set; }

        public override string ToString()
        {
            string vehicleStr = $"Type: {(this.Type == "car" ? "Car" : "Truck")}{Environment.NewLine}" +
                                $"Model: {this.Model}{Environment.NewLine}" +
                                $"Color: {this.Color}{Environment.NewLine}" +
                                $"Horsepower: {this.HorsePower}";

            return vehicleStr;
        }
    }
}

проблема беше в отпечатването накрая виж тук: https://softuni.bg/forum/answers/details/53090

0