如何控制存储在数组中的数据
How to control data stored in an array
抱歉,我想将特定输入 data/numbers 存储在一个数组中。在我的阵列中,我只想存储正确的数字,而不是一次错误的数字。只要我的数组中没有得到 4 个正确的数字,我就希望 proram 继续。
然后我想打印出数组值的总数。
我如何在 C# 中执行此操作?这是我的代码:
int min = 5;
int max = 10;
int[] array = new int[4];
int count = 0;
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("enter btw 5 och 10");
int val = int.Parse(Console.ReadLine());
array[i] = val;
if (val >= min && val <= max)
{
Console.WriteLine("Correct, continue...");
count++;
continue;
}
else
{
Console.WriteLine("wrong, enter btw 5 och 10");
continue;
}
}
Console.WriteLine(count);
Console.ReadKey();
}
如果有人输入例如 11 或 2,程序将不会计算在内。
谢谢你的帮助。
您可以尝试类似的方法:
int min = 5;
int max = 10;
int[] array = new int[4];
int index = 0;
while(true)
{
Console.WriteLine("enter btw 5 och 10");
int val = int.Parse(Console.ReadLine());
if (val >= min && val <= max)
{
Console.WriteLine("Rätt siffra...");
array[index] = val;
if(index >= array.Length-1)
break;
index++;
}
else
{
Console.WriteLine("wrong, enter btw 5 och 10");
}
}
Console.WriteLine(index);
Console.ReadKey();
最好用int.TryParse()
(就像我会写的那样)
int min = 5;
int max = 10;
int[] array = new int[4];
int count = 0;
while(true)
{
int val;
// read the input
Console.WriteLine("enter btw 5 och 10");
string input = Console.ReadLine();
// parse it
if(!int.TryParse(input, out val))
{
Console.WriteLine("Not a valid number");
continue;
}
// check the range
if (val < min || val > max)
{
Console.WriteLine("wrong, enter btw 5 och 10");
continue;
}
// store it.
Console.WriteLine("Rätt siffra...");
array[count] = val;
// are we done?
if(count== array.Length-1)
// yes, break the while loop before we increase.
break;
// increase array index
count++;
}
Console.WriteLine(count);
Console.ReadKey();
有效吗?
{
var min = 5;
var max = 10;
var array = new int[4];
var count = 0;
var total = 0;
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter between 5 and 10:");
var correct = false;
while (!correct)
{
var val = int.Parse(Console.ReadLine());
if (val >= min && val <= max)
{
Console.WriteLine("Right number...");
array[i] = val; //Add the value to the array
count++; //Increase count
total += val; //Add the value to total
correct = true; //Break the while loop
}
else
{
Console.WriteLine("Wrong, enter between 5 and 10:");
}
}
}
Console.WriteLine($"The total of the values are: {total}");
Console.ReadKey();
}
抱歉,我想将特定输入 data/numbers 存储在一个数组中。在我的阵列中,我只想存储正确的数字,而不是一次错误的数字。只要我的数组中没有得到 4 个正确的数字,我就希望 proram 继续。 然后我想打印出数组值的总数。 我如何在 C# 中执行此操作?这是我的代码:
int min = 5;
int max = 10;
int[] array = new int[4];
int count = 0;
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("enter btw 5 och 10");
int val = int.Parse(Console.ReadLine());
array[i] = val;
if (val >= min && val <= max)
{
Console.WriteLine("Correct, continue...");
count++;
continue;
}
else
{
Console.WriteLine("wrong, enter btw 5 och 10");
continue;
}
}
Console.WriteLine(count);
Console.ReadKey();
}
如果有人输入例如 11 或 2,程序将不会计算在内。 谢谢你的帮助。
您可以尝试类似的方法:
int min = 5;
int max = 10;
int[] array = new int[4];
int index = 0;
while(true)
{
Console.WriteLine("enter btw 5 och 10");
int val = int.Parse(Console.ReadLine());
if (val >= min && val <= max)
{
Console.WriteLine("Rätt siffra...");
array[index] = val;
if(index >= array.Length-1)
break;
index++;
}
else
{
Console.WriteLine("wrong, enter btw 5 och 10");
}
}
Console.WriteLine(index);
Console.ReadKey();
最好用int.TryParse()
(就像我会写的那样)
int min = 5;
int max = 10;
int[] array = new int[4];
int count = 0;
while(true)
{
int val;
// read the input
Console.WriteLine("enter btw 5 och 10");
string input = Console.ReadLine();
// parse it
if(!int.TryParse(input, out val))
{
Console.WriteLine("Not a valid number");
continue;
}
// check the range
if (val < min || val > max)
{
Console.WriteLine("wrong, enter btw 5 och 10");
continue;
}
// store it.
Console.WriteLine("Rätt siffra...");
array[count] = val;
// are we done?
if(count== array.Length-1)
// yes, break the while loop before we increase.
break;
// increase array index
count++;
}
Console.WriteLine(count);
Console.ReadKey();
有效吗?
{
var min = 5;
var max = 10;
var array = new int[4];
var count = 0;
var total = 0;
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter between 5 and 10:");
var correct = false;
while (!correct)
{
var val = int.Parse(Console.ReadLine());
if (val >= min && val <= max)
{
Console.WriteLine("Right number...");
array[i] = val; //Add the value to the array
count++; //Increase count
total += val; //Add the value to total
correct = true; //Break the while loop
}
else
{
Console.WriteLine("Wrong, enter between 5 and 10:");
}
}
}
Console.WriteLine($"The total of the values are: {total}");
Console.ReadKey();
}