C# 如何在数组C中搜索一系列值#

C# 如何在数组C中搜索一系列值#,c#,arrays,search,C#,Arrays,Search,我对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 a

我对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);
    }
}
class总线
{
私有字符串name=“minibus”;
公共国际机场[]乘客=新国际机场[9];
公共交通;
public void Run()//这是菜单,您可以填写年龄,然后使用其他选项遍历数组
{
Console.WriteLine(“迷你巴士模拟器”);
内部温度=0;
做
{
WriteLine(“选择一个备选方案(从1;-)开始)”;
控制台。写线(“1.填写通行证”);
控制台。WriteLine(“2.打印乘客年龄”);
Console.WriteLine(“3.显示所有乘客的总年龄”);
控制台。书写线(“4.乘客的平均年龄”);
Console.WriteLine(“5.谁的年龄最大”);
Console.WriteLine(“6.显示年内的乘客”);
Console.WriteLine(“7.按年龄对旅客进行分类”);
Console.WriteLine(“点击0退出\n”);
temp=int.Parse(Console.ReadLine());
开关(温度)
{
案例1:
添加乘客();
打破
案例2:
print_-bus();
打破
案例3:
计算总年龄();
打破
案例4:
计算平均年龄();
打破
案例5:
最大年龄();
打破
案例6:
查找年龄();
打破
案例7:
sort_buss();
打破
案例0:
Console.WriteLine(“按任意键继续”);
打破
违约:
Console.WriteLine(“使用0到7之间的数字”);
打破
}
}而(温度!=0);
}
公共空间添加乘客()
{
对于(int i=0;i=0&&result=keyMin&&passengers[i]passengers[j+1])
{
内部温度=乘客[j];
乘客[j]=乘客[j+1];
乘客[j+1]=温度;
}
}
}
对于(int i=0;i

}

如果中断for循环,则必须推迟中断:

    for (int i = 0; i < passengers.Length; i++)            
    {
        if (passengers[i] >=  keyMin && passengers[i] <= keyMax)   

        {
            Console.WriteLine("Those are the passangers within range: " + i);
        }
   ---> //break;
    }
for(int i=0;i=keyMin和乘客[i]//中断;
}

如果中断for循环,则必须推迟中断:

    for (int i = 0; i < passengers.Length; i++)            
    {
        if (passengers[i] >=  keyMin && passengers[i] <= keyMax)   

        {
            Console.WriteLine("Those are the passangers within range: " + i);
        }
   ---> //break;
    }
for(int i=0;i=keyMin和乘客[i]//中断;
}

Imo,Break;是复制粘贴的错误。作为一种改进,您应该将搜索与显示分离,这样如果有多个结果,您就不会显示“这些是乘客:a;这些是乘客:B;这些是乘客:C”,而是类似于“那些是乘客:a,B,C”在我看来,Break;是复制粘贴的一种拼写错误。作为一种改进,你应该将搜索与显示分离开来,这样,如果有多个结果,你就不会显示“那些是乘客:a;那些是乘客:B;那些是乘客:C”,而是类似于“那些是乘客:a,B,C”