我还是 C# 的新手,我被困在我的代码中的一个功能上。我无法让它返回“case6”中的值,在范围内搜索年龄(最小-最大),其他一切正常。那里有什么问题?
class Buss
{
private string name = "minibuss";
public int[] passengers = new int[9];
public int passengerNum;
public void Run() //that´s the menu, you fill in age and then could go through array with other options
{
Console.WriteLine("THE miniBUSS SIMULATOR");
int temp = 0;
do
{
Console.WriteLine("Choose an alternative (starting with 1 ;-))");
Console.WriteLine("1. Fill in pass");
Console.WriteLine("2. Print passengers age");
Console.WriteLine("3. Show total age of all passanger");
Console.WriteLine("4. Average age of passsangers");
Console.WriteLine("5. Who´s oldest");
Console.WriteLine("6. Show passangers within range of years");
Console.WriteLine("7. Sort passangers by age");
Console.WriteLine("Hit 0 to exit\n");
temp = int.Parse(Console.ReadLine());
switch (temp)
{
case 1:
add_passenger();
break;
case 2:
print_buss();
break;
case 3:
calc_total_age();
break;
case 4:
calc_average_age();
break;
case 5:
max_age();
break;
case 6:
find_age();
break;
case 7:
sort_buss();
break;
case 0:
Console.WriteLine("Press any key to continue");
break;
default:
Console.WriteLine("Use number between 0 and 7");
break;
}
} while (temp != 0);
}
public void add_passenger()
{
for (int i = 0; i < passengers.Length; i++)
{
Console.WriteLine("Age of passenger {0}", i);
if (int.TryParse(Console.ReadLine(), out int result) && result >= 0 && result <= 122)
{
passengers[i] = result;
}
else
{
i--;
Console.WriteLine("Max age is 122");
}
}
}
public void print_buss()
{
foreach (int temp in passengers)
{
Console.WriteLine(temp);
}
}
public void calc_total_age()
{
int sum = 0;
for (int i = 0; i < passengers.Length; i++)
{
sum += passengers[i];
}
Console.WriteLine("Total age is " + sum);
}
public void calc_average_age()
{
int sum = 0;
for (int i = 0; i < passengers.Length; i++)
{
sum += passengers[i];
}
int avr = sum / passengers.Length;
Console.WriteLine("Avarage age is " + avr);
}
public void max_age()
{
int max = passengers.Max();
Console.WriteLine(max);
}
public void find_age()
{
Console.WriteLine("Write the lower limit for your range: ");
int keyMin = int.Parse(Console.ReadLine()); // say 10
Console.WriteLine("Write the highest value for your range: ");
int keyMax = int.Parse(Console.ReadLine()); // ex. 30 so range is 10 - 30
for (int i = 0; i < passengers.Length; i++)
{
if (passengers[i] >= keyMin && passengers[i] <= keyMax)
//I´ve tried different options for that loop but without succes, do I
// need to call sort_buss() method first?
{
Console.WriteLine("Those are the passangers within range: " + i);
}
break;
}
Console.WriteLine("Nothing to return\n"); //That´s what program returns each time
}
public void sort_buss()
{
int max = passengers.Length - 1;
for (int i = 0; i < max; i++)
{
int nrLeft = max - i;
for (int j = 0; j < nrLeft; j++)
{
if (passengers[j] > passengers[j + 1])
{
int temp = passengers[j];
passengers[j] = passengers[j + 1];
passengers[j + 1] = temp;
}
}
}
for (int i = 0; i < passengers.Length; i++)
Console.WriteLine("\n Passengers sorted by age ", passengers[i]);
}
}
class Program
{
public static void Main(string[] args)
{
var minbuss = new Buss();
minbuss.Run();
Console.ReadKey(true);
}
}
}