111,129
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace search
{
public partial class Form1 : Form
{
private int id = 0;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)//读取输入数据
{
id = Convert.ToInt32(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)//开始查找
{
int data = id;
}
private int search(int data, int[] array)//二分法
{
int low = 0;
int high = array.Length;
int mid;
while (low != high)
{
mid = (low + high) / 2;
if (array[mid] == data) return data;
else
{
if (array[mid] > data) { high = mid - 1; }
else if (array[mid] < data) { low = mid + 1; }
}
}
return 0;
}
}
}
private void button1_Click(object sender, EventArgs e)//开始查找
{
int data = id;
int[] array = { 1, 2, 3 ,4};//数组列表
int location = search(id, array);
if (location >= 0)
{
MessageBox.Show(location.ToString()+被查找出来!, "查找结果");//显示查找到的数据
}
else
{
MessageBox.Show("表中不存在您要查找的数据", "查找结果");
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace search
{
public partial class Form1 : Form
{
private int id;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)//输入查找数据
{
id = Convert.ToInt32(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)//开始查找
{
int data = id;
int[] array = { 1, 2, 3 ,4};//数组列表
int location = search(id, array);
if (location >= 0)
{
MessageBox.Show("....", "查找结果");//显示查找到的数据
}
else
{
MessageBox.Show("表中不存在您要查找的数据", "查找结果");
}
}
private int search(int data, int[] array)//二分法
{
int low = 0;
int high = array.Length;
int mid;
while (low != high)
{
mid = (low + high) / 2;
if (array[mid] == data) return data;//返回查找到的值
else
{
if ((array[mid] > data && array[low] < data) || (array[mid] < data && array[high] < data))
{ high = mid - 1; }
else if ((array[mid] < data && array[high] > data) || (array[mid] > data && array[low] > data))
{ low = mid + 1; }
}
}
return -1;
}
}
}
修改后的代码 请问如何才能将查找出来的data在messagebox中显示出来