Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值读入数组_C# - Fatal编程技术网

C# 将值读入数组

C# 将值读入数组,c#,C#,此程序要求用户输入数组大小,然后要求用户输入数组值。我遇到的问题是,读取数组值的for循环不能正常工作。无论n的值是多少,它都会要求更多的输入 int n; Console.WriteLine("Enter how many values you are entering in"); n = Convert.ToInt32(Console.Read()); int[] arr = new int[n]; Console.WriteLine("Enter your values in");

此程序要求用户输入数组大小,然后要求用户输入数组值。我遇到的问题是,读取数组值的for循环不能正常工作。无论
n
的值是多少,它都会要求更多的输入

int n;

Console.WriteLine("Enter how many values you are entering in");
n = Convert.ToInt32(Console.Read());

int[] arr = new int[n];
Console.WriteLine("Enter your values in");

for (int i = 0; i < n; i++)
{
    arr[i] = Convert.ToInt32(Console.Read());
}
intn;
WriteLine(“输入输入的值数量”);
n=Convert.ToInt32(Console.Read());
int[]arr=新的int[n];
Console.WriteLine(“在中输入值”);
对于(int i=0;i
使用ReadLine()获取数字,然后在输入字符时使用ReadKey()not Read()或ReadLine()

int n;

Console.WriteLine("Enter how many values you are entering in");
n = Convert.ToInt32(Console.ReadLine());

int[] arr = new int[n];
Console.WriteLine("Enter your values in");

for (int i = 0; i < n; i++)
{
  char c = Console.ReadKey().KeyChar;
  arr[i] = Convert.ToInt32(c.ToString());
}
intn;
WriteLine(“输入输入的值数量”);
n=Convert.ToInt32(Console.ReadLine());
int[]arr=新的int[n];
Console.WriteLine(“在中输入值”);
对于(int i=0;i
使用
Console.ReadLine()
而不是
Console.Read()

如文档所述,Console.Read()只返回一个字符。您可能希望读入整行并解析整数值

此代码应执行您想要的操作:

int n;

Console.WriteLine("Enter how many values you are entering in");
//n = Convert.ToInt32(Console.Read());
n = Int32.Parse(Console.ReadLine());

int[] arr = new int[n];
Console.WriteLine("Enter your values in");

for (int i = 0; i < n; i++)
{
    arr[i] = Int32.Parse(Console.ReadLine());
}
intn;
WriteLine(“输入输入的值数量”);
//n=Convert.ToInt32(Console.Read());
n=Int32.Parse(Console.ReadLine());
int[]arr=新的int[n];
Console.WriteLine(“在中输入值”);
对于(int i=0;i

这也适用于n=10的输入情况

快速简便的修复方法:

使用
int.Parse(Console.ReadLine())
代替
Convert.ToInt32(Console.Read())

说明:


您正在以您的方式获取ASCII值。例如,如果您输入数字
2
,您的
n
变量实际上会被设置为
50

,这里有一个版本,其中包含一些更可靠的错误检查。 注意:(在不运行的情况下直接输入,因此如果有小的语法错误,我深表歉意)

int计数;
bool attemptedSetCount=false;
做
{
如果(尝试设置计数)
{
Console.WriteLine(“请输入有效的整数”);
}
WriteLine(“输入输入的值数量”);
string countString=Console.ReadLine();
attemptedSetCount=true;
}
而(!Int32.TryParse(countString,out count));
int[]arr=新的int[n];
Console.WriteLine(“在中输入值”);
for(int i=0;i
用于(int i=0;i
控制台.Read函数读取下一个输入字符,并将其作为
int
值返回。然后,代码将其传递给
Convert.ToInt32
API,该API将返回传入的确切值。这意味着像
1
这样具有字符值
49
的数字将被处理为
49
。这就是代码读取如此多值的原因

要从
控制台
正确读取数字,请使用
ReadLine
方法,该方法返回一个
字符串
。然后可以将其传递到
Int32.Parse
并转换为
int

Int32.Parse(Console.ReadLine());
或者更好的是,将其抽象为一种方法

static int ReadInt() {
  return Int32.Parse(Console.ReadLine());
}
?

Read
方法在您键入输入字符时阻止其返回; 当您按Enter键时,它终止。按Enter键可附加一个 输入端的平台相关线路终止顺序(例如, Windows附加一个回车换行符(换行顺序)对 Read方法每次检索输入的一个字符。在最后一个字符之后 检索字符时,读取将再次阻止其返回,循环将重复

请注意,除非执行以下操作之一,否则不会得到属性值-1 以下操作:同时按下控制修改器键和Z控制台 键(Ctrl+Z),表示文件状态结束;按等效键 表示文件状态结束的信号,如Windows中的F6功能键; 或者将输入流重定向到具有实际 文件结束字符

ReadLine
方法,或
KeyAvailable
属性和
ReadKey
方法 比使用读取方法更好

如果我执行此代码:

Console.Write("? ") ;
int input = Console.Read() ;
Console.WriteLine("You entered {0}.", input ) ;
Console.WriteLine( "{0} is the decimal code point for the character whose glyph is '{1}.'" , input , (char)input ) ;
如果我在
提示下输入字符
123
,然后按
返回键:

? 123<return>
[注意在Windows中,您可以通过按住
键,键入'0049
并释放
`键,在命令提示下生成'1'

假设用户的目的是指定要输入的多个值,然后提示他们输入这么多的输入值,那么您需要的代码如下所示:

static void Main()
{
  int   n      = ReadIntegerFromConsole( "How many values do you want to enter?" ) ;
  int[] values = new int[n] ;

  for ( int i = 0 ; i < values.Length ; ++i )
  {
    string prompt = string.Format( "{0}/{1}?" , i , n ) ;

    values[i] = ReadIntegerFromConsole(prompt) ;

  }

  Console.WriteLine( "You entered: {0}" , string.Join(", ",values) ) ;
  return ;
}

static int ReadIntegerFromConsole( string prompt )
{
  int  value   ;
  bool isValid ;

  do
  {

    Console.Write( prompt) ;
    Console.Write( ' ' );

    string text = Console.ReadLine() ;

    isValid = int.TryParse(text, out value ) ;

    prompt = "That's not an integer. Try again:" ;
  } while (!isValid) ;

  return value ;
}
static void Main()
{
int n=ReadIntegerFromConsole(“您想输入多少值?”);
int[]值=新的int[n];
对于(int i=0;i? 123<return>
You entered 49.
49 is the decimal code point for the character whose glyph is '1'.
static void Main()
{
  int   n      = ReadIntegerFromConsole( "How many values do you want to enter?" ) ;
  int[] values = new int[n] ;

  for ( int i = 0 ; i < values.Length ; ++i )
  {
    string prompt = string.Format( "{0}/{1}?" , i , n ) ;

    values[i] = ReadIntegerFromConsole(prompt) ;

  }

  Console.WriteLine( "You entered: {0}" , string.Join(", ",values) ) ;
  return ;
}

static int ReadIntegerFromConsole( string prompt )
{
  int  value   ;
  bool isValid ;

  do
  {

    Console.Write( prompt) ;
    Console.Write( ' ' );

    string text = Console.ReadLine() ;

    isValid = int.TryParse(text, out value ) ;

    prompt = "That's not an integer. Try again:" ;
  } while (!isValid) ;

  return value ;
}