问题
I start dicoverig C# today ... as you know there is some difuculties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[3];
int i;
for (i=0; i < 3; i++)
{
arr[i] = Console.Read();
}
for (int k = 0; k < 3; k++)
{
Console.Write(arr[k]);
}
Console.ReadKey();
}
}
}
this code didn't work with me when i compiled it ...it make me put value for one time then he print other values ! any one here to help
回答1:
try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[3];
int i;
for (i = 0; i < 3; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (int k = 0; k < 3; k++)
{
Console.WriteLine(arr[k]);
}
Console.ReadKey();
}
}
}
Bye.
回答2:
If i understand you correcrtly this is what you want
int[] arr = new int[3];
for (int i = 0; i < 3; i++)
arr[i] = Convert.ToInt32(Console.ReadLine());
for (int k = 0; k < 3; k++)
Console.WriteLine(arr[k]);
Console.ReadKey();
回答3:
Instead of
arr[i] = Console.Read();
try this:
arr[i] = (int)Console.ReadKey().KeyChar;
Console.Read() will continue to read the same character over and over as it will still be considered the next character from the input stream. The Console.ReadKey() function reads keys as they are pressed.
来源:https://stackoverflow.com/questions/1382515/how-can-read-values-and-put-them-in-array-c-sharp