6

C#でConsole.Read()とConsole.ReadLine()を使用しようとしていますが、奇妙な結果が得られています。たとえば、このコード

Console.WriteLine("How many students would you like to enter?");
int amount = Console.Read();
Console.WriteLine("{0} {1}", "amount equals", amount);

for (int i=0; i < amount; i++)
{
     Console.WriteLine("Input the name of a student");
     String StudentName = Console.ReadLine();
     Console.WriteLine("the Students name is " + StudentName);
}

生徒数に1を入力すると、その金額= 49になりますが、生徒名を入力する機会すらありません。

4

10 に答える 10

11

これは、文字を読み取るためです。ReadInt32()読み取りシンボルから希望する型への正しい変換を処理するような適切なメソッドを使用してください。

取得する理由49は、「1」記号の char コードであり、整数表現ではないためです。

char     code
0 :      48
1 :      49
2:       50
...
9:       57

例: 次のReadInt32()ようになります。

public static int ReadInt32(string value){
      int val = -1;
      if(!int.TryParse(value, out val))
          return -1;
      return val;
}

これを次のように使用します。

int val = ReadInt32(Console.ReadLine());

を作成する可能性があることは本当に素晴らしいことですがextension method、残念ながら、静的型で拡張メソッドを作成することはできず、Consolestatic型です。

于 2012-09-06T20:48:01.203 に答える
3

まだこれが必要な人のために:

static void Main(string[] args)
{
     Console.WriteLine("How many students would you like to enter?");
     var amount = Convert.ToInt32(Console.ReadLine());

     Console.WriteLine("{0} {1}", "amount equals", amount);

     for (int i = 0; i < amt; i++)
     {
         Console.WriteLine("Input the name of a student");
         String StudentName = Console.ReadLine();
         Console.WriteLine("the Students name is " + StudentName);
     }
}
于 2016-02-03T01:50:21.727 に答える
2

charint ではなく read から文字を取得します。最初に文字列にして、それを文字列として解析する必要があります。実装は以下のようになります

    Console.WriteLine("How many students would you like to enter?");
    var read = Console.ReadLine();
    int amount;
    if(int.TryParse(read,out amount)) {
      Console.WriteLine("{0} {1}", "amount equals", amount);

      for (int i=0; i < amount; i++)
      {
        Console.WriteLine("Input the name of a student");
        String StudentName = Console.ReadLine();
        Console.WriteLine("the Students name is " + StudentName);
      }
    }

readline は文字列を返し、学生の数を 9 (1 桁の最大数) に任意に制限しないため、readline を使用するように変更しました。

于 2012-09-06T20:54:35.350 に答える
0

それ以外の:

int amount = Console.Read();

試す:

int amount = 0;
int.TryParse(Console.ReadLine(), out amount);

文字コードだけを読み取るため、たとえば 11 と入力しても 49 が返されます。文字列値を読み取り、それを int 値に解析する必要があります。上記のコードでは、入力が悪い場合は 0 になります。

于 2012-09-06T20:56:03.427 に答える
0

Console.Read押されたキー文字の asci 値を返します。

を使用すると、押された実際の文字を表すConsole.ReadKey().KeyCharが得られます。char

を使用して、その文字を 1 文字の文字列に変換できます.ToString()

これで文字列ができたので、使用したり、完全に数字を含む文字列を整数に変換しint.Parseたりできます。int.TryParse

すべてをまとめると、次のようになります。

int value;
if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out value))
{
    //use `value` here
}
else
{
    //they entered a non-numeric key
}
于 2012-09-06T20:57:58.110 に答える
0

これを試して:

int amount = ReadInt32();

または、うまくいかない場合は、これを試してください:

int amount = Console.ReadInt32();

またはこれ:

int amount = Convert.ToInt32(Console.Readline());

これでは、文字列を読み取り、それを Int32 値に変換します。

ここにもアクセスできます: http://www.java2s.com/Tutorials/CSharp/System.IO/BinaryReader/C_BinaryReader_ReadInt32.htm

何もうまくいかない場合は、お知らせください。

于 2015-07-06T10:57:07.213 に答える
0

Console.Read()入力した文字のcharコードを返しています。文字を文字列として取得するために使用する必要があり、次に探している値を取得Convert.ToChar(amount);する必要があります。int.Parse()

于 2012-09-06T20:49:25.150 に答える