1 svar
82 visningar
mai2020 är nöjd med hjälpen!
mai2020 31
Postad: 26 feb 2020

Bussen projekt behöver hjälp!!!Nybörjare!!!!

Hej! Jag håller på att fixa en uppgift och jag behöver er hjälp!!!!

Min fråga är att(1) varför "public" framför "void find_age()" och "void sort_age()" visar innehållet nedan:

"the modifier public is not valid for this item"

(2)Det visar "find_age()" och "sort_age()" "does not exist in the current context"

 

Min kod är som dedan:

class Program
{
public static void Main(string[] args)
{
var minbuss = new Buss();
minbuss.Run();
Console.Write("Press any key to exit the program......");
Console.ReadKey();
}
}
}

class Buss
{
public int[] passenger = new int[25];
public int total_passenger;

 

public void Run()
{
int menu = 0;
do
{
Console.WriteLine("-----------------------------------------------------");
Console.WriteLine("Welcome to the bus!");
Console.WriteLine("Choose an alternative:");
Console.WriteLine("1. Add passenger");
Console.WriteLine("2. Check the age of the passengers on the bus");
Console.WriteLine("3. Calculate the average age of the passengers");
Console.WriteLine("4. Find the oldest passenger on the bus");
Console.WriteLine("5. Find the passengers within a specific range of age:");
Console.WriteLine("6. Sort the passengers by their ages");
Console.WriteLine("0. Exit the program!");
Console.WriteLine("-----------------------------------------------------");
menu = int.Parse(Console.ReadLine());
switch (menu)
{
case 1:
add_passenger();
break;

case 2:
print_buss();
break;

case 3:
calc_average_age();
break;

case 4:
max_age();
break;

case 5:
find_age();
break;

case 6:
sort_bus();
break;

}
} while (menu != 0);
}

public void add_passenger()
{
Console.WriteLine("How many passengers would you like to add?");
int size = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < size; i++)
{
Console.WriteLine("Write the age of the passenger you would like to add: ");
int pass = Convert.ToInt32(Console.ReadLine());//Konvertera string till int
passenger[i] = pass; //Adderar värdet till vektoren
}
}

public void print_buss()
{
Console.WriteLine("The ages of the passengers on the bus are: ");
for (int i = 0; i < passenger.Length; i++)
{
Console.WriteLine(passenger[i] + "years old");
}
}


public int calc_average_age()
{
int sum = 0;
for (int i = 0; i < passenger.Length; i++)
{
sum += passenger[i];
}
int averAge = sum / passenger.Length;
Console.WriteLine("The average age of the the passengers is " + averAge);
return averAge;
}

public int max_age()
{
int maxAge = 0;
maxAge = passenger[0];
for (int i = 0; i < passenger.Length; i++)
{
if (passenger[i] > maxAge)
{
maxAge = passenger[i];
}
}
Console.WriteLine("The oldest age is [0]", maxAge);
return maxAge;

public void find_age()
{
Console.WriteLine("What is the lowest age you would like to find?");
int lowAge = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is the highest age you would like to find?");
int highAge = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The passengers between" + lowAge + " - " + highAge + "years old are sitting on: ");
for (int i = 0; i < passenger.Length; i++)
{
if (passenger[i] > lowAge || passenger[i] < highAge)
{
Console.WriteLine("Seat " + i);
}
}
}

public void sort_bus()
{
for (int i = 0; i < passenger.Length - 1; i++)
{
for (int j = 0; j < passenger.Length - 1 - i; j++)
{
if (passenger[j] > passenger[j + 1])
{
int temp = passenger[j];
passenger[j] = passenger[j + 1];
passenger[j + 1] = temp;
}
}
}
for (int i = 0; i < passenger.Length; i++)
{
Console.WriteLine("Passenger " + (i + 1) + " is " + passenger[i] + "years old");
}

}

}
}

Laguna 7663
Postad: 26 feb 2020

Det fattas en högerklammer sist i funktionen max_age.

Om du har en editor som hjälper till med indentering så ser du det genast.

Svara Avbryt
Close