列表和数组C# [英] lists and arrays c#

查看:54
本文介绍了列表和数组C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我正在做一个日志,其中有不同选项的菜单.在菜单1中,用户输入他的帖子(标题和帖子).在菜单2中,您将可以查看所有已保存的帖子.因此,您应该能够看到所有已撰写的帖子,但只有一篇 显示的是最后的书面帖子.有人知道如何解决吗?

Hi!
I am doing a logbook, where there is a menu of different options. In menu 1, the user enters his post (Title and Post). In Menu 2 you will be able to view all saved posts. You should therefore be able to see ALL posts that have been written, but the only one shown is the last written post. Does anyone know how to solve this?

using System;
using System.Collections.Generic;
namespace ProjektLoggbok
{
	class Program
	{
		public static void Main(string[] args)

		{
			List<string[]> loggBok = new List<string[]> {}; 
			string[] post = new string[2];
			post[0] = "Titel";
			post[1] = "Inlägg";
			DateTime tiden = DateTime.Now;
			Console.WriteLine(tiden);
			bool isRunning = true;
			while (isRunning)      

			{

				Console.WriteLine("\n\tVälkommen till loggboken!"); //Här är hela min meny för programmet.
				Console.WriteLine("\t[1]Skriv ett inlägg: ");
				Console.WriteLine("\t[2]Skriv ut alla inlägg");
				Console.WriteLine("\t[3]Sök inlägg");
				Console.WriteLine("\t[4]Avsluta programmet!");
				Console.Write("\nVälj meny: ");

				int nr;
				int.TryParse(Console.ReadLine(), out nr); 
				switch (nr) 

				{
					case 1:
						
							
						Console.WriteLine("Skriv en titel till ditt inlägg: ");
						post[0] = Console.ReadLine();
						Console.WriteLine("Skriv ett inlägg: ");
						post[1] = Console.ReadLine();

						loggBok.Add(post);
						break;

					case 2:
						foreach (string[] text in loggBok) 						{
							Console.WriteLine("\nHär är dina inlägg i loggboken:\n ");
							Console.WriteLine("Inlägg:{0} "+"\n\t{1}",text);
						}
						break;



						case 3:
						Console.WriteLine("Skriv in ett ord för att söka bland dina inlägg");
						string keyword = Console.ReadLine();
						foreach (string[] text in loggBok)
						{
							if (post[0].Contains(keyword) || post[1].Contains(keyword)) 
							{
								Console.Write("\nTitel: " + post[0] + "\n" + post[1]);
							}

						}
						break;




					case 4:
						isRunning = false;
						break;

					default:
						Console.WriteLine("Försök igen, välj mellan 1-4!");
						break;

				}

			}


		}
	}
}





推荐答案

您的基本问题是,整个程序只有一个字符串数组,该字符串数组是在此行中创建的:

string[] post = new string[2];

然后,将对该字符串的引用一遍又一遍地存储在日志中.  您每次都需要创建一个新数组.  这是一种可能的方法,

You then store a reference to that string over and over in your log book.  You need to create a new array each time.  Here is one possible approach, which :

//string[] post = new string[2];
string title;
string content;

case 1:												
	Console.WriteLine("Skriv en titel till ditt inlägg: ");
	title = Console.ReadLine();
	Console.WriteLine("Skriv ett inlägg: ");
	content = Console.ReadLine();

	loggBok.Add(new string[] {title, content});
	break;


这篇关于列表和数组C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆