如何在 C# 中删除重复的数字 (int)
如何使用 for
循环删除列表框中的重复项。我尝试了几种方法,但它们不起作用。我尝试了最后一种方法,但不是删除重复项,而是将它们替换为 0。
我的作业是在列表框中生成随机数,并在单独的列表框中分隔为偶数和奇数。然后我必须将平均值和选定的数字计算为立方和平方根。一切正常。除了重复的。需要一些帮助。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssignmentQ1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGenerate_Click(object sender, EventArgs e)
{
List<int> lstRN = new List<int>();
Random rnd = new Random();
int numbers = rnd.Next(1, 1000);
int item = 0;
int digit = 0;
int totalOdd, totalEven, countEven, countOdd;
totalOdd = totalEven = countEven = countOdd = 0;
double avg = 0.0;
double sum = 0.0;
for (int i=0; i<numbers; i++)
{
item = rnd.Next(1, 300);
lstRN.Add(item);
}
lstRN.Sort();
digit = lstRN[0];
for (int k= 0; k<lstRN.Count; k++)
{
if(lstRN[k] == digit)
{
lstRN[k]=0;
}
else
{
digit = lstRN[k];
}
}
foreach (int num in lstRN)
{
if(num % 2 == 0)
{
lstEven.Items.Add(num);
totalEven += num;
}
else
{
lstOdd.Items.Add(num);
totalOdd += num;
}
}
/**var _items1 = this.lstRN.Items.Cast<string>().Distinct().ToList();
this.lstRN.Items.Clear();
foreach (var item1 in _items1)
{
this.lstEven.Items.Add(item1);
}
var _items2 = this.lstRN.Items.Cast<string>().Distinct().ToList();
this.lstRN.Items.Clear();
foreach (var item2 in _items2)
{
this.lstOdd.Items.Add(item2);
}**/
countEven = lstEven.Items.Count;
lblCountEven.Text = countEven.ToString();
avg = item / countEven;
lblAverageEven.Text = avg.ToString();
countOdd = lstOdd.Items.Count;
lblCountOdd.Text = countOdd.ToString();
avg = item / countOdd;
lblAverageOdd.Text = avg.ToString();
}
private void lstEven_SelectedIndexChanged(object sender, EventArgs e)
{
string chosen = lstEven.SelectedItem.ToString();
int c = int.Parse(chosen);
double sr = Math.Sqrt(c);
lblSquare.Text = sr.ToString();
}
private void lstOdd_SelectedIndexChanged(object sender, EventArgs e)
{
string chosen = lstOdd.SelectedItem.ToString();
int c = int.Parse(chosen);
double cube = Math.Pow(c, 3);
lblCube.Text = cube.ToString();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
How to remove duplicated in a listbox using for
loop. I have tried few methods but they are not working. I've tried a last method but instead of removing the duplicates, they are replaced with 0.
My assignment is about generating a random numbers in a listbox and separating to even and odd numbers in a separate listbox. Then i have to calculate the average and selected numbers into cube and sqroot. Everything is working fine. Except the duplicates. Need some help.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssignmentQ1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGenerate_Click(object sender, EventArgs e)
{
List<int> lstRN = new List<int>();
Random rnd = new Random();
int numbers = rnd.Next(1, 1000);
int item = 0;
int digit = 0;
int totalOdd, totalEven, countEven, countOdd;
totalOdd = totalEven = countEven = countOdd = 0;
double avg = 0.0;
double sum = 0.0;
for (int i=0; i<numbers; i++)
{
item = rnd.Next(1, 300);
lstRN.Add(item);
}
lstRN.Sort();
digit = lstRN[0];
for (int k= 0; k<lstRN.Count; k++)
{
if(lstRN[k] == digit)
{
lstRN[k]=0;
}
else
{
digit = lstRN[k];
}
}
foreach (int num in lstRN)
{
if(num % 2 == 0)
{
lstEven.Items.Add(num);
totalEven += num;
}
else
{
lstOdd.Items.Add(num);
totalOdd += num;
}
}
/**var _items1 = this.lstRN.Items.Cast<string>().Distinct().ToList();
this.lstRN.Items.Clear();
foreach (var item1 in _items1)
{
this.lstEven.Items.Add(item1);
}
var _items2 = this.lstRN.Items.Cast<string>().Distinct().ToList();
this.lstRN.Items.Clear();
foreach (var item2 in _items2)
{
this.lstOdd.Items.Add(item2);
}**/
countEven = lstEven.Items.Count;
lblCountEven.Text = countEven.ToString();
avg = item / countEven;
lblAverageEven.Text = avg.ToString();
countOdd = lstOdd.Items.Count;
lblCountOdd.Text = countOdd.ToString();
avg = item / countOdd;
lblAverageOdd.Text = avg.ToString();
}
private void lstEven_SelectedIndexChanged(object sender, EventArgs e)
{
string chosen = lstEven.SelectedItem.ToString();
int c = int.Parse(chosen);
double sr = Math.Sqrt(c);
lblSquare.Text = sr.ToString();
}
private void lstOdd_SelectedIndexChanged(object sender, EventArgs e)
{
string chosen = lstOdd.SelectedItem.ToString();
int c = int.Parse(chosen);
double cube = Math.Pow(c, 3);
lblCube.Text = cube.ToString();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您是 LINQ 爱好者,您可以使用以下扩展方法
它将收集所有相等的项目并选择其中的第一个
If you are a LINQ addict, you can use the following extension method
It will collect all the items that are the equals and select the first of them
有专用的工具/类型 -
HashSet
。要在集合中收集唯一的随机数,
.Add
方法仅当值不在集合中时才会添加给定值。哈希集。添加(T)方法
There are dedicated tool/type for that -
HashSet<int>
.To gather unique random numbers in the collection
.Add
method will add given value only if value is not in the set yet.HashSet.Add(T) Method