6 svar
157 visningar
samah95 1
Postad: 26 nov 2023 20:17

I tried to solve the task but it says there is an error that the program did not accept

using System;

class City
{
private string namn;
private int temp;

public City(string namn, int temp)
{
this.namn = namn;
this.temp = temp;
}

public override string ToString()
{
return $"City: {namn}, Temperatur: {temp} grader Celsius";
}
}

class Program
{
static int LinearSearch(City[]cities, int n, int söktemp)
{
for (int i = 0; i < n; i++)
{
if (cities[i].ToString().Contains($"Temperatur: {söktemp}"))
{
return i; // Returnera indexet om temperaturen hittas
}
}

return -1; // Returnera -1 om temperaturen inte hittas
}

static void BubbleSort(City[] cities, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
// compare the temperatures and switch places if the first is higher and the second
if (cities[j].ToString().Contains("Temperatur") && cities[j + 1].ToString().Contains("Temperatur"))
{
int temp1 = int.Parse(cities[j].ToString().Split(':')[1].Trim().Split(' ')[0]);
int temp2 = int.Parse(cities[j + 1].ToString().Split(':')[1].Trim().Split(' ')[0]);

if (temp1 < temp2)
{
// switch place
City tempCity = cities[j];
cities[j] = cities[j + 1];
cities[j + 1] = tempCity;
}
}
}
}
}

static void Main()
{
// Declare a field Cities with four cities
City[] cities = new City[4];

// Let the user feed in cities och temperaturer
for (int i = 0; i < 4; i++)
{
Console.Write($"Enter namn för city {i + 1}: ");
string namn = Console.ReadLine();

Console.Write($"Enter temperatures for city {i + 1}: ");
int temp = int.Parse(Console.ReadLine());

cities[i] = new City(namn, temp);
}

// Sort the field cities
BubbleSort(cities, 4);

// Prent the cities in order of temperature
Console.WriteLine("\nCities efter temperatursortering:");
foreach (var city in cities)
{
Console.WriteLine(city.ToString());
}

// the user enters the temperature to search for
Console.Write("\nEnter temperatur to search after: ");
int searchtemp = int.Parse(Console.ReadLine());

// Call LinearSearch and print the result
int result = LinearSearch(cities, 4, searchtemp);
if (result != -1)
{
Console.WriteLine($"\nCity with temperatur {searchtemp}was found on index {result}.");
}
else
{
Console.WriteLine($"\nno with temperatur {searchtemp} found on.");
}
}
}

Laguna Online 28602
Postad: 27 nov 2023 17:26

Vad är felmeddelandet?

anders_k Online 234
Postad: 29 nov 2023 19:04 Redigerad: 29 nov 2023 19:12

If you indent your code properly, you will notice there is missing a closing bracket

class Program
{
  static int LinearSearch(City[]cities, int n, int söktemp)
  {
    for (int i = 0; i < n; i++)
    {
      if (cities[i].ToString().Contains($"Temperatur: {söktemp}"))
      {
        return i; // Returnera indexet om temperaturen hittas
      }
    }
    return -1; // Returnera -1 om temperaturen inte hittas

  // here missing }
}

Also your main looks a bit suspicious

It should look something like this

class HelloWorld {
  static void Main() {
    
     // your code


  }
}
Soderstrom 2767
Postad: 29 nov 2023 19:19

Jag är inte bra när det kommer till programmering men jag fick lära mig att klippa ut koden och klistra in typ en rad i taget tills man får ett fel meddelande!

Vad säger folket om det?

Laguna Online 28602
Postad: 29 nov 2023 19:32

En vettig kompilator säger vilken rad det är fel på.

Soderstrom 2767
Postad: 29 nov 2023 19:51
Laguna skrev:

En vettig kompilator säger vilken rad det är fel på.

Finns det en sån för Matlab?

Jag är bara van att jobba med Matlab. Jag introducerades till programmeringsvärlden först på universitetet och genom Matlab, så därav har jag inte så bra koll på andra prog. språk :)

Laguna Online 28602
Postad: 29 nov 2023 20:00

Jag har ingen erfarenhet av felhantering i Matlab.

Svara Avbryt
Close