C# 将元素输入数组并检查元素是否已存在
我正在解决一个问题,我尝试了我能想到的各种方法,使用for循环,但我不知道如何使它工作,因为我几周前才开始使用c#和整个编程 编写一个输入五个数字的应用程序。当读取每个数字时,如果该数字在数组中不存在,则搜索该数组,输出单词“new”,并将该数字插入数组。如果该数字确实存在于数组中,则输出“存在”。输入所有五个数字后,输出数组的内容 这就是我目前所拥有的。谢谢你的帮助C# 将元素输入数组并检查元素是否已存在,c#,arrays,C#,Arrays,我正在解决一个问题,我尝试了我能想到的各种方法,使用for循环,但我不知道如何使它工作,因为我几周前才开始使用c#和整个编程 编写一个输入五个数字的应用程序。当读取每个数字时,如果该数字在数组中不存在,则搜索该数组,输出单词“new”,并将该数字插入数组。如果该数字确实存在于数组中,则输出“存在”。输入所有五个数字后,输出数组的内容 这就是我目前所拥有的。谢谢你的帮助 using System; public class Program { // Main method begins e
using System;
public class Program
{
// Main method begins execution of C# application
public static void Main(string[] args)
{
int[] array = new int[5];
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter a number:");
array[i] = Convert.ToInt32(Console.ReadLine());
for (int a = 0; a < 5; a++)
{
if (array[i] != array[a])
{
array[i] = int.Parse(Console.ReadLine());
Console.WriteLine("new\n");
}
}
array[i] = int.Parse(Console.ReadLine());
Console.WriteLine("exists\n");
}
Console.WriteLine(array);
Console.ReadKey();
}
} // end class
使用系统;
公共课程
{
//Main方法开始执行C#应用程序
公共静态void Main(字符串[]args)
{
int[]数组=新的int[5];
for(int i=0;i
您的代码有几个问题。只有在成功的情况下才应该增加数组索引-否则缺少的值将为零。如果用户输入了无效值,验证输入也是一个好主意。可以使用linq检查数组中是否已存在该值:
下面是一个例子:
static void Main(string[] args)
{
int[] array = new int[5];
var index = 0;
while (index < array.Length)
{
Console.WriteLine("Enter a number:");
var input = Console.ReadLine();
var value = 0;
if (!int.TryParse(input, out value))
{
Console.WriteLine("Error - value entered was not a number");
}
else
{
var match = array.Where(a => a == value);
if (match.Any())
{
Console.WriteLine("exists\n");
}
else
{
array[index] = value;
Console.WriteLine("new\n");
index++;
}
}
}
foreach (var item in array)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static void Main(字符串[]args)
{
int[]数组=新的int[5];
var指数=0;
while(索引<数组长度)
{
Console.WriteLine(“输入一个数字:”);
var input=Console.ReadLine();
var值=0;
如果(!int.TryParse(输入,输出值))
{
WriteLine(“错误-输入的值不是数字”);
}
其他的
{
var match=array,其中(a=>a==value);
if(match.Any())
{
Console.WriteLine(“存在\n”);
}
其他的
{
数组[索引]=值;
Console.WriteLine(“新建”);
索引++;
}
}
}
foreach(数组中的变量项)
{
控制台写入线(项目);
}
Console.ReadKey();
}
首先,在您真正开始编写代码之前,请尝试思考一个解决方案,并给出一些提示
Contains
方法完成计数
等于5
供参考使用,以及
试试这个:
var numbers = new HashSet<int>();
while(numbers.Count < 5)
{
Console.WriteLine("Enter a number:"); //1.
var number = Convert.ToInt32(Console.ReadLine());
if (numbers.Contains(number)) // 2.
{
Console.WriteLine("exists\n"); //3.
continue;
}
Console.WriteLine("new\n"); //4.
numbers.Add(number);
}
foreach (var n in numbers)
{
Console.WriteLine(n);
}
var numbers=newhashset();
while(number.Count<5)
{
Console.WriteLine(“输入一个数字:”);//1。
var number=Convert.ToInt32(Console.ReadLine());
if(numbers.Contains(number))//2。
{
Console.WriteLine(“存在\n”);//3。
继续;
}
Console.WriteLine(“新的\n”);//4。
数字。添加(数字);
}
foreach(数值中的变量n)
{
控制台写入线(n);
}
在系统.Linq
中使用.Any()
。此外,您不需要在循环中不断获取用户输入:
using System;
using System.Linq;
public class Program
{
// Main method begins execution of C# application
public static void Main(string[] args)
{
int[] array = new int[5];
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter a number:");
// Get user input and convert to integer:
int input = Convert.ToInt32(Console.ReadLine());
// Check if given input is already in the array:
if (! array.Any(number => number == input))
{
array[i] = input;
Console.WriteLine("new\n");
}
else
{
Console.WriteLine("exists\n");
}
}
// Print the contents of array separated by ','
Console.WriteLine(string.Join(", ", array));
Console.ReadKey();
}
}
使用系统;
使用System.Linq;
公共课程
{
//Main方法开始执行C#应用程序
公共静态void Main(字符串[]args)
{
int[]数组=新的int[5];
for(int i=0;inumber==input))
{
数组[i]=输入;
Console.WriteLine(“新建”);
}
其他的
{
Console.WriteLine(“存在\n”);
}
}
//打印以“,”分隔的数组内容
Console.WriteLine(string.Join(“,”,数组));
Console.ReadKey();
}
}
编辑:如果希望用户完全填充阵列,则另一个变量:
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
// Btw, if this is not an array of nullables,
// we will not be able to insert a zero later, as
// we will treat them as duplicates.
int?[] array = new int?[5];
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter a number:");
int input = 0;
bool duplicateAttempt = false;
do {
// Get and convert the input.
input = Convert.ToInt32(Console.ReadLine());
// See if this number is already in.
duplicateAttempt = array.Contains(input);
// Report if we attempt to insert a duplicate.
if (duplicateAttempt) Console.WriteLine("exists");
}
while (duplicateAttempt); // Keep asking while we don't get a unique number.
array[i] = input; // Store the number
Console.WriteLine("new");
}
// Print the contents of array separated by ','
Console.WriteLine(string.Join(", ", array));
Console.ReadKey();
}
}
使用系统;
使用System.Linq;
公共课程
{
公共静态void Main(字符串[]args)
{
//顺便说一句,如果这不是一个空值数组,
//我们以后将无法插入零,因为
//我们将把它们当作复制品。
int?[]数组=新int?[5];
for(int i=0;i
这似乎是一个家庭作业问题,所以我认为解释你做错了什么并告诉你你做了什么更有帮助
using System;
public class Program
{
// Main method begins execution of C# application
public static void Main(string[] args)
{
int[] array = new int[5];
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter a number:");
// you are putting the value into your array here. so it will always 'exist' below
array[i] = Convert.ToInt32(Console.ReadLine());
// you need to do this check before you insert the number into the array
// put the ReadLine into a variable, not directly into the array.
// then check if it's in the array already
for (int a = 0; a < 5; a++)
{
if (array[i] != array[a])
{
// here you are trying to read another value from the user.
// Console.ReadLine() will always wait for user input
// use the variable from above. but only after you have looped through all the items.
// and verified that it is not present
array[i] = int.Parse(Console.ReadLine());
Console.WriteLine("new\n");
}
}
// this is the third time you have assigned the number to the array
// and the third time you've asked for user input per loop
array[i] = int.Parse(Console.ReadLine());
Console.WriteLine("exists\n");
}
Console.WriteLine(array);
Console.ReadKey();
}
} // end class