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 _10个随机二位数_并找最大数的位置
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void label1_Click(object sender, EventArgs e)
{ Random rnd=new Random ();
int[] a = new int[10];
int max;
for(int i=0;i<10;i++)
{ a[i] = rnd.Next(10, 99);
label1.Text = a[i] + " ";
max = a[0];
if (a[i] > max)
max = a[i];
int x= Array.BinarySearch(a,max);
label1.Text +="\n\n最大的两位数位于第"+x+"个单元的位置";
}
}
}
}题目是:编写程序,为数组A赋予10个随机二位整数,找出具有最大值的元素并指示其位置。但我运行出来的位置,居然有在第-8个单元的位置。而且那10个随机数并不在标签上面显示。请教。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace _10个随机二位数_并找最大数的位置
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void label1_Click(object sender, EventArgs e)
{ Random rnd=new Random ();
int[] a = new int[10];
int max;
for(int i=0;i<10;i++)
{ a[i] = rnd.Next(10, 99);
label1.Text = a[i] + " ";
max = a[0];
if (a[i] > max)
max = a[i];
int x= Array.BinarySearch(a,max);
label1.Text +="\n\n最大的两位数位于第"+x+"个单元的位置";
}
}
}
}题目是:编写程序,为数组A赋予10个随机二位整数,找出具有最大值的元素并指示其位置。但我运行出来的位置,居然有在第-8个单元的位置。而且那10个随机数并不在标签上面显示。请教。
label1.Text = a[i] + " ";
改成
label1.Text += a[i] + " ";max = a[0];
应该是i=0时这样写其他的就不用了
void Main()
{
Random rnd=new Random ();
int[] a = new int[10];
int max=0;
int x=0;
string result="";
for(int i=0;i<10;i++)
{ a[i] = rnd.Next(10, 99);
result+= a[i].ToString() + " "; //你的错误在这点
max = a[0];
if (a[i] > max)
max = a[i];
}
x= Array.BinarySearch(a,max);
result+="\n\n最大的两位数位于第"+x+"个单元的位置";
Console.WriteLine(result);
}
/*
56 70 55 95 23 97 72 89 27 82 最大的两位数位于第-6个单元的位置*/
int[] a = new int[10];
int max = 0;
int x = 0;
string result = "";
for (int i = 0; i < 10; i++)
{ a[i] = rnd.Next(10, 99); result += a[i].ToString() + " "; //你的错误在这点
}
max = a[0];
for (int i = 0; i < 10; i++)
{
if (a[i] > max)
max = a[i];
}
x = Array.BinarySearch(a, max);
result += "\n\n最大的两位数位于第" + x + "个单元的位置";
Console.WriteLine(result);