[問題] 新手試做通訊錄
小的剛開始學C# (以前完全沒碰過程式...)
是一邊翻書一邊寫的,
這次是要寫一個簡單的通訊錄,
規定是這樣的:
制定手機類別,類別中之欄位與方法符合自己的需求。
讓使用者可以輸入友人電話及姓名。
讓使用者可以查詢友人的電話。
可是寫了之後有兩個比較重要的問題不知道該怎麼解決,
身邊也沒有人可以問ˊˋ
1. 在新增幾個聯絡人之後,如果我想查詢聯絡人,
而要查的人是放在新增陣列裡的話,他會跑出5個查無此人,
然後跑出一個"未處理的例外狀況...索引在陣列的界線外"
2. 如果要查詢的連絡人是在已經有寫資料的陣列裡的話,
他會跑出人名和電話沒錯,可是也會跑出5個查無此人,
也會出現和第一個問題一樣的錯誤
我試著用我所想的到的方式去改了,
可是越用越糟,我實在不知道該怎麼修他 Q Q
關於我設定的class很詭異的問題....
呃...因為實在想不出要怎麼用上,只好先這樣寫,
好像沒有造成error,所以就只好先暫時讓他長這樣了 ╮(╯__╰)╭
底下附上完整程式 ˊˋ
----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class phone
{
public static string[] Name;
public static string[] Num;
}
class Program
{
static void Main(string[] args)
{
for (; ; )
{
int j = 0;
string[] inN = new string[90];
string[] inNum = new string[90];
Console.WriteLine("==歡迎進入通訊錄==\n");
string[] Name = { "apple", "Ben", "Merry", "John", "lulu" };
String[] Num = { "0945999236", "0912984592", "0928499328",
"0929448104", "0948222941" };
Console.WriteLine("姓名\t 電話號碼");
Console.WriteLine("==================");
Array.Sort(Num, Name);
int i;
for (i = 0; i <= Name.GetUpperBound(0); i++)
{
Console.WriteLine("{0}\t{1}", Name[i], Num[i]);
}
string yes = "";
Console.WriteLine("\n是否要繼續新增聯絡人(Y/N)?");
yes = Console.ReadLine();
if (yes == "y" || yes == "Y")
{
do
{
Console.WriteLine("\n請輸入姓名");
inN[j] = Console.ReadLine();
Console.WriteLine("請輸入電話");
inNum[j] = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("\n是否要繼續新增聯絡人(Y/N)?");
yes = Console.ReadLine();
}
while (yes == "y" || yes == "Y");
}
Console.WriteLine("\n請選擇您要的動作");
Console.WriteLine("1.查詢聯絡人 2.撥號 3.離開 (3.)\n");
Console.Write("選擇→\t");
String Ans = Console.ReadLine();
if (Ans == "1")
{
Console.WriteLine("\n==通訊錄查詢==\n");
Console.WriteLine("姓名\t 電話號碼");
Console.WriteLine("==================");
Console.WriteLine("輸入欲查詢姓名");
string find = Console.ReadLine();
Array.Sort(inNum, inN);
for (int k = 0; k <= inN.GetUpperBound(0); k++)
{
if (find == inN[k])
{
Console.WriteLine("{0}\t{1}", inN[k], inNum[k]);
}
else if (find == Name[k])
{
Console.WriteLine("{0}\t{1}", Name[k], Num[k]);
}
else
{
Console.WriteLine("查無此人!!");
}
}
}
else if (Ans == "2")
{
Console.WriteLine("\n請鍵入電話號碼");
String Call = Console.ReadLine();
Console.WriteLine("\n正在撥號給{0}\n", Call);
}
if (Ans == "3" || Ans != "1" || Ans != "2")
{
break;
}
Console.WriteLine("\n正在離開程式\n");
}
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.240.117
推
04/12 01:16, , 1F
04/12 01:16, 1F
→
04/12 01:17, , 2F
04/12 01:17, 2F
→
04/12 01:18, , 3F
04/12 01:18, 3F
→
04/12 02:11, , 4F
04/12 02:11, 4F
→
04/12 08:35, , 5F
04/12 08:35, 5F
→
04/12 08:36, , 6F
04/12 08:36, 6F
→
04/12 08:37, , 7F
04/12 08:37, 7F
推
04/12 21:16, , 8F
04/12 21:16, 8F
→
04/12 21:16, , 9F
04/12 21:16, 9F
→
04/12 22:41, , 10F
04/12 22:41, 10F
→
04/12 22:42, , 11F
04/12 22:42, 11F
→
04/12 22:44, , 12F
04/12 22:44, 12F
→
04/12 22:45, , 13F
04/12 22:45, 13F
→
04/12 23:33, , 14F
04/12 23:33, 14F
→
04/12 23:33, , 15F
04/12 23:33, 15F
→
04/13 01:02, , 16F
04/13 01:02, 16F
→
04/13 01:04, , 17F
04/13 01:04, 17F