如何从用户处获取数组并向其打印重复的数字,以及重复的数字有多少?C#
我试图让用户输入一些数字,我应该找出重复的数字以及重复的数字的数量。因此,如果他键入2三次,我应该告诉他2是重复的数字,并且出现了三次如何从用户处获取数组并向其打印重复的数字,以及重复的数字有多少?C#,c#,C#,我试图让用户输入一些数字,我应该找出重复的数字以及重复的数字的数量。因此,如果他键入2三次,我应该告诉他2是重复的数字,并且出现了三次 using System; using System.Collections.Generic; using System.Threading; namespace Task4 { class Program { static void Main(string[] arg
using System;
using System.Collections.Generic;
using System.Threading;
namespace Task4
{
class Program
{
static void Main(string[] args)
{
int i, j;
Console.WriteLine("Type repeating numbers and I will find them: ");
int size = Convert.ToInt32(Console.In.ReadLine());
int[] num = new int[size];
for (i = 0; i < num.Length; i++)
{
num[i] = Convert.ToInt32(Console.In.ReadLine());
for (j = i + size; j < num.Length; j++)
{
if (num[i] == num[j])
Console.Write(num[i] + " ");
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统线程;
命名空间任务4
{
班级计划
{
静态void Main(字符串[]参数)
{
int i,j;
WriteLine(“键入重复的数字,我会找到它们:”);
int size=Convert.ToInt32(Console.In.ReadLine());
int[]num=新的int[size];
对于(i=0;i
下面是一个使用GroupBy
int[] arr = { 1, 2, 3, 4,2, 5, 6, 5, 5 };
var frequency = arr.GroupBy(x => x).Select(x => new { x.Key, count = x.Count() });
我将使用a存储与输入的每个数字相关联的计数
输出:
Type repeating numbers, one per line, and I will find them ('quit' to end).
Number ('quit' to end): 1
1 added
Number ('quit' to end): 2
2 added
Number ('quit' to end): 3
3 added
Number ('quit' to end): 2
2 added
Number ('quit' to end): 4
4 added
Number ('quit' to end): 2
2 added
Number ('quit' to end): 5
5 added
Number ('quit' to end): 6
6 added
Number ('quit' to end): 7
7 added
Number ('quit' to end): 6
6 added
Number ('quit' to end): 8
8 added
Number ('quit' to end): quit
Results:
Number: 2, Occurrences: 3
Number: 6, Occurrences: 2
Press Enter to quit...
代码:
static void Main(字符串[]args)
{
字典计数=新字典();
int i;
字符串输入;
WriteLine(“键入重复的数字,每行一个,我会找到它们('quit'结束)。”;
做
{
控制台。写入(“编号('quit'结束):”;
input=Console.ReadLine();
if(int.TryParse(输入,输出i))
{
如果(!counts.ContainsKey(i))
{
counts.Add(i,1);//我们第一次看到这个数字
}
其他的
{
计数[i]=计数[i]+1;//增加此数字的计数
}
控制台写入线(i+“添加”);
}
否则如果(输入!=“退出”)
{
Console.WriteLine(“无效整数”);
}
}while(输入!=“退出”);
Console.WriteLine(“结果:”);
foreach(KeyValuePair kvp计数)
{
如果(kvp.Value>1)
{
Console.WriteLine(“编号:+kvp.Key+”,出现次数:+kvp.Value);
}
}
控制台。写入(“按Enter键退出…”);
Console.ReadLine();
}
我会使用。您似乎没有编写任何试图查找重复数字的代码。“你为什么不试一下呢?”itsme86那么我该如何使用它呢?因为我对c#@AhmedAdel没有什么经验,所以我将您链接到了文档,其中甚至有如何使用它的示例。“我不打算为你做家庭作业。@梅森,我搞不懂,因为我对c#没有什么经验。我想让用户输入数字是的,这样你就可以将用户输入值添加到数组或列表中,然后使用此方法。”
static void Main(string[] args)
{
Dictionary<int, int> counts = new Dictionary<int, int>();
int i;
string input;
Console.WriteLine("Type repeating numbers, one per line, and I will find them ('quit' to end).");
do
{
Console.Write("Number ('quit' to end): ");
input = Console.ReadLine();
if (int.TryParse(input, out i))
{
if(!counts.ContainsKey(i))
{
counts.Add(i, 1); // first time we've seen this number
}
else
{
counts[i] = counts[i] + 1; // increment the count for this number
}
Console.WriteLine(i + " added");
}
else if (input != "quit")
{
Console.WriteLine("Invalid Integer");
}
} while (input != "quit");
Console.WriteLine("Results:");
foreach(KeyValuePair<int, int> kvp in counts)
{
if (kvp.Value > 1)
{
Console.WriteLine("Number: " + kvp.Key + ", Occurrences: " + kvp.Value);
}
}
Console.Write("Press Enter to quit...");
Console.ReadLine();
}