static void AddContact()
{
string path = @"C:\Users\...\Desktop\Projekte\C# übungen\ContactList/contacts.txt";
string name, address;
int phoneNumber;
bool addingContact = true;
string response;
List<string> ContactList = File.ReadAllLines(path).ToList();
while(addingContact)
{
System.Console.Write("Name: ");
name = Console.ReadLine();
System.Console.Write("Nummer: ");
phoneNumber = int.Parse(Console.ReadLine());
System.Console.Write("Adresse: ");
address = Console.ReadLine();
ContactList.Add($"Name: {name}");
ContactList.Add($"Nummer: {phoneNumber}");
ContactList.Add($"Adresse: {address} \n---------------------------------");
foreach (var contact in ContactList)
{
File.WriteAllLines(path, ContactList);
}
Console.Clear();
System.Console.WriteLine("Contact added successfully! Would you like to Add another Contact? (y)(n)");
response = Console.ReadLine().ToLower();
if(response == "y")
{
Console.Clear();
}
else
{
addingContact = false;
}
}
Console.WriteLine("Good bye!");
}
static void ShowContact()
{
string path = @"C:\Users\...\Desktop\Projekte\C# übungen\ContactList/contacts.txt";
string response;
System.Console.WriteLine("Would you like to see all contacts (0) or a specific one (1)?");
response = Console.ReadLine();
switch (response)
{
case "0":
File.ReadAllLines(path);
break;
case "1":
System.Console.WriteLine("Type the name of the user: ");
string username = Console.ReadLine();
File.ReadAllLines(path)
break;
}
}
這就是一切。在 case 1 的 switch 陳述句中,問題出在哪里。如果用戶鍵入 John,我希望它看起來像這樣:
姓名:John
編號:233
地址:lolol 23
我已經嘗試過一些東西,但我在寫它的程序中停下來,因為我意識到我實際上并不知道如何撰寫代碼,所以我前往 stackoverflow 尋求幫助,謝謝. 請有禮貌,我知道這段代碼不是最好的,我是初學者。
編輯:我剛剛發現 ShowContacts 根本不起作用
編輯 2:用 foreach 回圈和串列修復它
List<string> contacts = File.ReadAllLines(path).ToList();
System.Console.WriteLine("Would you like to see all contacts (0) or a specific one (1)?");
response = Console.ReadLine();
switch (response)
{
case "0":
foreach (var contact in contacts)
{
Console.WriteLine(contact);
}
break;
}
uj5u.com熱心網友回復:
將您的聯系人保存為 json 格式,如下所示:
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.IO;
namespace JsonTester
{
// Open the Package Manager Console:
// VS2022 >> Tools >> Nuget Package Manager >> Package Manager Console
//
// Install libraries through Package Manager Console (look at bottom of VS2022):
// PM > Install-Package System.Text.Json -Version 6.0.1
// After type above, then press Enter to install json
internal class Program
{
static List<Contact> contactList = new List<Contact>();
static readonly string MY_CONTACT = @"D:\MyContact.txt";
class Contact {
public string Name { get; set; }
public string Number { get; set; }
public string Address { get; set; }
}
static void EnterContact() {
while (true)
{
Console.WriteLine("Enter Contact? [Y/N]");
string key = Console.ReadLine();
if (key.ToLower() == "y")
{
var contact = new Contact();
Console.WriteLine("Name: ");
contact.Name = Console.ReadLine();
Console.WriteLine("Number: ");
contact.Number = Console.ReadLine();
Console.WriteLine("Address: ");
contact.Address = Console.ReadLine();
contactList.Add(contact);
}
else {
break;
}
}
}
static void SaveContact() {
if (contactList.Count > 0) {
foreach (Contact contact in contactList) {
String strContactJson = JsonSerializer.Serialize(contact);
File.AppendAllText(MY_CONTACT, strContactJson "\n");
}
}
}
static void ReadContact()
{
try
{
String[] strContactJsonArr = File.ReadAllLines(MY_CONTACT);
if (strContactJsonArr.Length > 0)
{
foreach (String s in strContactJsonArr)
{
Contact contact = JsonSerializer.Deserialize<Contact>(s);
if (contact != null)
{
Console.WriteLine("Name: " contact.Name);
Console.WriteLine("Number: " contact.Number);
Console.WriteLine("Address: " contact.Address);
Console.WriteLine("");
}
}
}
}
catch (Exception) {
}
}
static void Main(string[] args)
{
ReadContact();
EnterContact();
SaveContact();
}
}
}
控制臺輸出:

MyContact.txt(聯系人保存為 JSON 格式)

uj5u.com熱心網友回復:
好的,如果每個專案都被三個值分解,請考慮將資料分成三個而不是迭代結果。
將下面的 Debug.WriteLine 更改為 Console.WriteLine 以獲取您的代碼。
分塊的擴展方法
public static class ListExtensions
{
public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
=> source
.Select((value, index) => new { Index = index, Value = value })
.GroupBy(x => x.Index / chunkSize)
.Select(grp => grp.Select(v => v.Value).ToList())
.ToList();
}
檔案內容
John
111
John's address
Mary
222
Mary's address
Bill
333
Bill's address
首先斷言資料的代碼可被三整除,而不是讀取資料,不斷言資料型別或執行空值。
var contents = File.ReadAllLines("TextFile1.txt");
if (contents.Length % 3 == 0)
{
var results = contents.ToList().ChunkBy(3);
foreach (var list in results)
{
Debug.WriteLine($" Name: {list[0]}");
Debug.WriteLine($" Number: {list[1]}");
Debug.WriteLine($"Address: {list[2]}");
}
}
else
{
Debug.WriteLine("Malformed");
}
編輯:放入方法中
private static void ViewAllContacts()
{
var fileName = "TextFile1.txt";
if (File.Exists(fileName))
{
var contents = File.ReadAllLines(fileName);
if (contents.Length % 3 == 0)
{
var results = contents.ToList().ChunkBy(3);
foreach (var list in results)
{
Debug.WriteLine($" Name: {list[0]}");
Debug.WriteLine($" Number: {list[1]}");
Debug.WriteLine($"Address: {list[2]}");
}
}
else
{
Debug.WriteLine("Malformed");
}
}
else
{
Debug.WriteLine($"{fileName} not found");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/419869.html
標籤:
上一篇:查詢資料庫中整個表的特定列
