2017-03-20 18 views
-1

私は学校のプロジェクトのためにピザ注文プログラムを作成しようとしています。これらの情報はすべて文字列配列に格納されますが、電話番号セクションには数字だけを入力することができます。私は提供されているメソッドを使用してみましたが、それは単にエラーを動作させるように見えません。ここでコンソールアプリケーションの文字列配列に数字だけを格納する方法

私の現在のコード

ここ
public static string[] GetCustInfo(string [] custInfo) 
    { 
     start: 
     bool success = false; 

     Console.Write("\n" + "Please input D for Delivery OR P for Pickup($3 for Delivery): " + "\n"); 
     custInfo[0] = Console.ReadLine(); 

     while (success != true) 
     { 

      if (custInfo[0] == "D" || custInfo[0] == "d") 
      { 

       custInfo[1] = ReadString("Please enter your name: "); 
       Console.Write(Readint("Please enter your Ph No. : ")); 
       custInfo[2] = Console.ReadLine(); 
       custInfo[3] = ReadString("Please enter your adress: "); 


       success = true; 
      } 
      else if (custInfo[0] == "P" || custInfo[0] == "p") 
      { 
       custInfo[1] = ReadString("Please enter your name: "); 

       success = true; 
      } 
      else 
      { 
       goto start; 
      } 

     } 

     return custInfo; 
    } 

は、私は数字または文字を入力することからユーザーを防ぐために使用していた方法ですされています

public static int Readint(String prompt) 
    { 
     int userInput = 0; 
     Boolean success = false; 
     while (!success) 
     { 
      Console.Write(prompt); 
      try 
      { 
       userInput = Convert.ToInt32(Console.ReadLine()); 
       success = true; 
      } 
      catch 
      { 
       Console.WriteLine("Please Enter a VALID NUMBER"); 
      } 
     } 
     return userInput; 
    } 

public static string ReadString(String prompt) 
    { 
     string userInput = " "; 
     Boolean success = false; 
     while (!success) 
     { 
      Console.Write(prompt); 
      try 
      { 
       userInput = Console.ReadLine(); 
       if (userInput.Length <= 20 && userInput.Length>1) 
       { 

        success = true; 

        foreach (char charcter in userInput) 
        { 
         if (Char.IsNumber(charcter) || Char.IsSymbol(charcter) || Char.IsPunctuation(charcter)) 
         { 
          success = false; 
         } 
        } 
       } 
       if (success == false) 
       { 
        Console.WriteLine("Please enter a valid input!" + "\n"); 
       } 
       else 
       { 
        success = true; 
       } 
      } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 
     return userInput; 
    } 

私は挿入しようとした:

custInfo[2] = Readint("Please enter your Ph No."); 

しかし、それは私にエラーを言ってくれました:

「という文字列にint型を巻き込むことはできない」

+1

あなたの質問に答えられませんが、 "goto"ステートメントをわずかにリファクタリングされたループに置き換えることを検討してください。 –

+0

あなたのピザは必須です。ほとんど無料でお届けします。 :) –

+0

私はちょうどそれをコーディングしている私の考えではなく、学校の割り当てのための要件。 – Jay

答えて

1

が、どのように私は番号が電話番号に入力するだけを許可するようになります。

ReadIntメソッドは既に処理されていますwhileループとtry/catch節のポイントです。この情報を配列に格納する場合は、stringという配列の正しい/適合データ型に番号を取得する必要があります。 ReadIntの戻り値はintであり、このクラスはToStringメソッドの実装を持っています。この場合、intstringに変換されます。だから、単にReadIntの戻り値でそれを呼び出すことができます。

custInfo[1] = ReadString("Please enter your name: "); 
custInfo[2] = Readint("Please enter your Ph No.").ToString(); 
custInfo[3] = ReadString("Please enter your adress: "); 
0

public static string[] GetCustInfo(string [] custInfo)のInsted、あなたは、オブジェクトの配列を追加するかもしれませんが、次の点に注意してください。異なる種類のオブジェクト配列ストアの要素を単一のコレクションに。オブジェクト参照は任意の派生型インスタンスを指すことができます。

あなたがこれを行う可能性がどのような:あなたのケースでそう

object[] array1 = new object[5]; 
array1[0] = new object(); 
    array1[1] = new StringBuilder("Initialized"); 
    array1[2] = "String literal"; 
    array1[3] = 3; 
    array1[4] = null; 

を:これは、この

public static object[] GetCustInfo(object [] custInfo) 
0

あなたはarray..surelyあなたが文字列に値を変換意味をなさない文字列にint型の値を挿入しようとしていますか?

関連する問題