2017-11-24 1 views
-2

コンパイルすると、配列の情報は表示されません。名前、電話番号、住所は表示されません。空白になるだけです。私はforループを使っていろいろな例を追ってきました。私はそれをうまく動作させることができず、エラーメッセージもありません。 私は誰の入力を感謝します。C#を使用して、名前を持つ文字列配列を使用してforループを使用して2D配列をループしようとしています。コンパイル時に情報が表示されない

string[,] customers = { { "Jay" , "123 Fake Street" , "212 111 1111"}, 
{" Pete" , "123 Fake Rd", "212 222 2222" }, 
{ "Will" , "112 Fake Av" , "212 333 3333"}, }; 
string cusName, 
cusInfo; 
bool phoneNumberFound = true; 

Write("Enter your phone #:") 
cusInfo = Convert.ToString(ReadLine()); 
for (int row = 0; row < customers.GetLength(0); ++row)  
{ 
    // Retreving the customers info from the array (second dimension of rows' width) 
    for (int col = 0; col < customers.GetLength(1); ++col) 
    { 
     if (phoneNumberFound) 
     { 
      cusInfo = customers[col, 0]; 

      WriteLine(); 
      WriteLine("Customer Name:\t", customers[row, col]); 
      WriteLine("Address:\t", customers[row, col]); 
      WriteLine("Phone Number:\t", customers[row, col]); 
      WriteLine(); 
      //WriteLine("{0} , what size pizza do you want?:"); 
      ReadLine(); 
     } 
     else 
     { 
      WriteLine("Your phone number was not found , please enter your name:\t"); 
      cusName = Convert.ToString(ReadLine()); 
     } 
    } 
} 
+0

ことの一つは、あなたが 'cusInfo = Convert.ToString(ReadLineメソッド())と' cusInfo'に顧客の入力を読んでいるということです; '、その後' cusInfoは、[顧客を=とそれをつかいますcol、0];そして、とにかくそれを使うことはありません。 – jdgregson

+0

'customers [col、0]'はcolを最初のランクとして使用しますが、これは他の用途と矛盾します。これは、cusInfoが使用されていないため問題ありません。 WriteLineステートメントが文字列内で指定された引数を使用していないため、空白の出力が得られる理由があります。あなたは、WriteLine( "Customer Name:\ t {0}"、customers [row、col]) 'または' WriteLine( "Customer Name:\ t" + customers [row、col])のいずれかを意味します。 –

+0

あなたは何をしたいですか?達成? 2D配列から詳細を印刷するだけです。質問を投稿する前にチュートリアルをチェックする場合は、 –

答えて

0
using System; 

namespace TwoDArray_47478344 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      string[,] customers = { { "Jay", "123 Fake Street", "212 111 1111" }, { " Pete", "123 Fake Rd", "212 222 2222" }, { "Will", "112 Fake Av", "212 333 3333" } }; 
      string cusInfo; 
      bool phoneNumberFound = false; 

      Console.Write("Enter your phone #:"); 
      cusInfo = Convert.ToString(Console.ReadLine()); 

      //loop through your customers 
      for (int i = 0; i < customers.Length; i++) 
      { 
       if (customers[i,2].ToString() == cusInfo)//if you find a matching phone number 
       { 
        Console.WriteLine(); 
        Console.WriteLine("Customer Name:\t{0}", customers[i, 0]); 
        Console.WriteLine("Address:\t{0}", customers[i, 1]); 
        Console.WriteLine("Phone Number:\t{0}", customers[i, 2]); 
        Console.WriteLine(); 
        Console.ReadLine(); 
        phoneNumberFound = true;//set the trigger to true 
        break;//stop looping 
       } 
      } 
      if (!phoneNumberFound) 
      { 
       Console.WriteLine("Your phone number could not be found."); 
      } 
     } 
    } 
} 

は、ここでそれを行うための代替方法です。私は得ることはありません

using System; 
using System.Collections.Generic; 

namespace TwoDArray_47478344 
{ 
    class Program 
    { 
     public static List<CustomerInfo> CustomerList = new List<CustomerInfo>();//the list that will contain our customers(instead of the 2d array, I'm using a custom class). 

     static void Main(string[] args) 
     { 

      MakeSomeCustomers();//populate the list of customers to cross-reference 

      string cusProvidedInfo;//we'll use and re-use this to query the list of customers 

      Console.Write("Enter your phone #:");//ask the user for phone 
      cusProvidedInfo = Convert.ToString(Console.ReadLine());//ask the user for phone 

      CustomerInfo currentCustomer = CustomerList.Find(p => p.Phone == cusProvidedInfo);//check if the supplied phone number is in our list of customers 

      if (currentCustomer == null)//if we don't have a customer, the phone number wasn't found, lets ask for first name 
      { 
       Console.WriteLine("\nCould not find you by phone number.\nPlease try entering your first name."); 
       cusProvidedInfo = Console.ReadLine().Trim(); 
       currentCustomer = CustomerList.Find(p => p.Name == cusProvidedInfo); 
      } 

      if (currentCustomer == null)//well... we couldn't find the customer based on Phone number, nor based on first name 
      { 
       Console.WriteLine("Still can't find you. Looks like you'll have to sign up."); 
      } 
      else 
      { 
       //we did find the customer, we can show the customer info 
       Console.WriteLine(); 
       Console.WriteLine("Customer Name:\t{0}", currentCustomer.Name); 
       Console.WriteLine("Address:\t{0}", currentCustomer.Address); 
       Console.WriteLine("Phone Number:\t{0}", currentCustomer.Phone); 
       Console.WriteLine(); 
      } 
      Console.ReadLine(); 

     } 


     /// <summary> 
     /// just creating some customers to search against 
     /// </summary> 
     private static void MakeSomeCustomers() 
     { 
      CustomerList.Add(new TwoDArray_47478344.CustomerInfo { Name = "Jay", Address = "123 Fake Street", Phone = "212 111 1111" }); 
      CustomerList.Add(new TwoDArray_47478344.CustomerInfo { Name = "Pete", Address = "123 Fake Road", Phone = "212 222 2222" }); 
      CustomerList.Add(new TwoDArray_47478344.CustomerInfo { Name = "Will", Address = "123 Fake Av", Phone = "212 333 3333" }); 
     } 
    } 

    /// <summary> 
    /// Instead of using a 2d array, I create a custom class to host the customer info 
    /// </summary> 
    public class CustomerInfo 
    { 
     public string Name { get; set; } 
     public string Address { get; set; } 
     public string Phone { get; set; } 
    } 
} 
関連する問題