请问一下,下面这个代码错误在哪里?
类A里的功能都不能用,是不是传值不成功啊!
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace shuzu
{
class A
{
public int[] p=new int[5];
public void paixu(int[] arr)//排序功能
{
for (int i = 0; i < p.Length; i++)
{
for (int j = i + 1; j < p.Length; j++)
if (p[i] > p[j])
{
int t = p[i];
p[i] = p[j];
p[j] = t;
}
Console.WriteLine("{0}", p[i]);
}
}
public void chazhao(int[] arr)//查找功能
{
string t = Console.ReadLine();
int L = Convert.ToInt32(t);
int i = 0;
int j = p.Length;
while (L < (i + j) / 2)
{
j = (j + i) / 2 - 1;
}
while (L > (i + j) / 2)
{
i = (j + i) / 2 + 1;
}
if (i > j)
Console.WriteLine("没有这个元素");
else
Console.WriteLine("{0}", (i + j) / 2);
}
public void qiuhe(int[] arr)//求和
{
int mux=0;
for(int i=0;i<p.Length;i++)
mux +=p[i];
Console.WriteLine("{0}", mux);
}
}
class Program
{
public static void Main()
{
string[] str = new string[5];
int[] arr = new int[5];
for (int i = 0; i < 5; i++)
{
str[i] = Console.ReadLine();
arr[i] = Convert.ToInt32(str[i]);
}
A sz = new A();
Console.WriteLine(" 1=paixu 2=chazhao 3=qiuhe");
Console.Write("Please enter your selection: ");
sz.paixu(arr);
sz.chazhao(arr);
sz.qiuhe(arr);
Console.ReadLine();
}
}
}
------解决方案-------------------- public void paixu(int[] arr)//排序功能
{
for (int i = 0; i < p.Length; i++)
{
for (int j = i + 1; j < p.Length; j++)
if (p[i] > p[j])
{
int t = p[i];
p[i] = p[j];
p[j] = t;
}
Console.WriteLine("{0}", p[i]);
}
}
=========================================================
你传的是arr,结果代码里面用的是数组p