C# 向每个用户询问指定问题的控制台应用程序
我正在尝试在C#中创建一个控制台应用程序,要求用户先登录,然后处理,只回答每个用户指定的问题。最好的方法是什么?我有四个问题,我只想问第一个用户前两个问题,第二个用户另外两个问题 下面是我的代码:C# 向每个用户询问指定问题的控制台应用程序,c#,C#,我正在尝试在C#中创建一个控制台应用程序,要求用户先登录,然后处理,只回答每个用户指定的问题。最好的方法是什么?我有四个问题,我只想问第一个用户前两个问题,第二个用户另外两个问题 下面是我的代码: static void Main(string[] args) { String username; String password; int row; string[,] accnts = { { "jack", "111", "1" }, { "ibo", "121
static void Main(string[] args)
{
String username;
String password;
int row;
string[,] accnts = { { "jack", "111", "1" }, { "ibo", "121", "2" } };
Console.Write("enter username >> ");
username = Console.ReadLine();
Console.Write("enter password >> ");
password = Console.ReadLine();
for (row = 0; row < 3; row++)
{
if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
{
Console.WriteLine("welcome " + accnts[row, 0]);
}
else if (username.Equals(accnts[row + 1, 0]) && password.Equals(accnts[row + 1, 1]))
{
Console.WriteLine("welcome " + accnts[row + 1, 0]);
}
else
{
Console.WriteLine("invalid access");
break;
}
string[,] question_id = { { "1", "1" }, { "1", "2" }, { "2", "3" }, { "2", "4" } };
string[,] questions = { { "Türkiyenin baskenti neresidir?", "1" }, { "Baskomutan kim?", "2" }, { "2 kere 2?", "3" }, { "when did the world war 1 start?", "4" } };
string[,] Answers = { { "a)ankara b)istanbul c)izmir", "1" }, { "a)ismet b)Atatürk c)Ali ", "2" }, { " a)1 b)2 c)4 ", "3" }, { " a)1912 b)1914 c)1915", "4" } };
string[,] trueAnswers = { { "a", "1" }, { "b", "2" }, { "c", "3" }, { "c", "4" } };
int result = 0;
string answers = "";
for (int i = 0; i < questions.GetLength(0); i++)
{
Console.WriteLine(questions[i, 0]);
Console.WriteLine("--------------------------");
for (int y = 0; y < Answers.GetLength(0); y++)
{
if (Answers[y, 1] == questions[i, 1])
{
Console.WriteLine(Answers[y, 0]);
answers = Console.ReadLine();
for (int z = 0; z < trueAnswers.GetLength(0); z++)
{
if (trueAnswers[z, 1] == questions[i, 1])
{
if (trueAnswers[z, 0] == answers)
result = result + 10;
Console.WriteLine("total is " + result);
}
}
}
}
}
if (result < 20)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
return;
}
}
static void Main(字符串[]args)
{
字符串用户名;
字符串密码;
int行;
字符串[,]accnts={{“jack”,“111”,“1”},{“ibo”,“121”,“2”};
控制台。写入(“输入用户名>>”;
username=Console.ReadLine();
控制台。写入(“输入密码>>”;
password=Console.ReadLine();
用于(行=0;行<3;行++)
{
if(username.Equals(账户[row,0])和password.Equals(账户[row,1]))
{
Console.WriteLine(“欢迎”+accnts[row,0]);
}
else if(username.Equals(账户[row+1,0])和password.Equals(账户[row+1,1]))
{
Console.WriteLine(“欢迎”+accnts[row+1,0]);
}
其他的
{
Console.WriteLine(“无效访问”);
打破
}
字符串[,]问题id={{“1”,“1”},{“1”,“2”},{“2”,“3”},{“2”,“4”};
字符串[,]问题={{“Türkiyenin baskenti neresidir?”,“1”},{“Baskomutan kim?”,“2”},{“2 kere 2?”,“3”},{“第一次世界大战是什么时候开始的?”,“4”};
字符串[,]答案={{a)安卡拉b)伊斯坦布尔c)伊兹密尔,{a)伊斯梅特b)阿塔图尔克c)阿里,{a)1b)2c)4”,“3},{a)1912b)1914c)1915c),4};
字符串[,]trueAnswers={{“a”,“1”},{“b”,“2”},{“c”,“3”},{“c”,“4”};
int结果=0;
字符串答案=”;
for(inti=0;i
首先让我告诉你完全错误的方法。这是我能做的最脏、最快的修复:
static void Main(string[] args)
{
String username;
String password;
int row;
string[,] accnts = { { "jack", "111", "1" }, { "ibo", "121", "2" } };
Console.Write("enter username >> ");
username = Console.ReadLine();
Console.Write("enter password >> ");
password = Console.ReadLine();
for (row = 0; row < 3; row++)
{
if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
{
Console.WriteLine("welcome " + accnts[row, 0]);
}
else if (username.Equals(accnts[row, 0]) && password.Equals(accnts[row, 1]))
{
Console.WriteLine("welcome " + accnts[row + 1, 0]);
}
else
{
Console.WriteLine("invalid access");
// changed break to continue, because it was crashing
continue;
}
string[,] question_id = { { "1", "1" }, { "1", "2" }, { "2", "3" }, { "2", "4" } };
string[,] questions = { { "Türkiyenin baskenti neresidir?", "1" }, { "Baskomutan kim?", "2" }, { "2 kere 2?", "3" }, { "when did the world war 1 start?", "4" } };
string[,] Answers = { { "a)ankara b)istanbul c)izmir", "1" }, { "a)ismet b)Atatürk c)Ali ", "2" }, { " a)1 b)2 c)4 ", "3" }, { " a)1912 b)1914 c)1915", "4" } };
string[,] trueAnswers = { { "a", "1" }, { "b", "2" }, { "c", "3" }, { "c", "4" } };
int result = 0;
string answers = "";
// here I've added a start and end thingy to offset the loop acording to logged user.
int start = 0;
int endModifier = 2;
if (username == accnts[1,0])
{
start = 2;
endModifier = 0;
}
for (int i = start; i < questions.GetLength(0) - endModifier; i++)
{
Console.WriteLine(questions[i, 0]);
Console.WriteLine("--------------------------");
for (int y = 0; y < Answers.GetLength(0); y++)
{
if (Answers[y, 1] == questions[i, 1])
{
Console.WriteLine(Answers[y, 0]);
answers = Console.ReadLine();
for (int z = 0; z < trueAnswers.GetLength(0); z++)
{
if (trueAnswers[z, 1] == questions[i, 1])
{
if (trueAnswers[z, 0] == answers)
result = result + 10;
Console.WriteLine("total is " + result);
}
}
}
}
}
if (result < 20)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
return;
}
}
问题的内部类:
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
}
填充这些类很容易,与您的示例几乎相同,但名称更加精巧。列表是你们的朋友,你们可以把所有账户都存储在字段中,但并没有那个些讨厌的索引
if可能有点复杂,但另一种方法是使用CheckUserPassword方法返回帐户,或者使用它输出参数。。。这是相当多的信息,所以我保留原样
现在,完成的代码如下所示:
static void Main(string[] args)
{
List<Account> accountsList = new List<Account>();
accountsList.Add(new Account { UserName = "jack", Password = "111", Group = 1 });
accountsList.Add(new Account { UserName = "ibo", Password = "121", Group = 2 });
Console.Write("enter username >> ");
string username = Console.ReadLine();
Console.Write("enter password >> ");
string password = Console.ReadLine();
if (CheckUserPassword(accountsList, username, password))
{
List<Question> questions = FillQuestions();
int result = 0;
for (int i = 0; i < questions.Count; i++)
{
if (questions[i].DesiredGroup == accountsList.Find(x => x.UserName == username).Group)
{
continue;
}
Console.WriteLine();
Console.WriteLine(questions[i].QuestionText);
Console.WriteLine("--------------------------");
WriteAnswers(questions[i].AnswersList);
string answers = Console.ReadLine();
for (int j = 0; j < questions[i].AnswersList.Count; j++)
{
if (questions[i].AnswersList[j].AcceptableLetter == answers)
{
if (questions[i].AnswersList[j].IsCorrect)
{
Console.WriteLine(questions[i].AnswersList[j].AcceptableLetter + " is correct");
result += 10;
}
else
{
Console.WriteLine(questions[i].AnswersList[j].AcceptableLetter + " is incorrect");
}
}
}
}
if (result < 15)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
}
Console.Read();
}
private static void WriteAnswers(List<Answer> answersList)
{
char[] alphabetLetters = { 'a', 'b', 'c' };
for (int i = 0; i < answersList.Count; i++)
{
Console.WriteLine(alphabetLetters[i] + ") " + answersList[i].AnswerText);
}
}
private static List<Question> FillQuestions()
{
List<Question> questionList = new List<Question>();
List<Answer> answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "Ankara", IsCorrect = true, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Istambul", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Izmir", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Türkiyenin baskenti neresidir?", AnswersList = answerList, DesiredGroup = 1 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "ismet", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Atatürk", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Ali", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Baskomutan kim?", AnswersList = answerList, DesiredGroup = 1 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "2", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "4", IsCorrect = true, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "2 kere 2?", AnswersList = answerList, DesiredGroup = 2 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1912", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "1914", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "1915", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "When did the world war 1 start?", AnswersList = answerList, DesiredGroup = 2 });
return questionList;
}
private static bool CheckUserPassword(List<Account> accountsList, string username, string password)
{
foreach (Account account in accountsList)
{
if (account.UserName == username)
{
if (account.Password == password)
{
Console.WriteLine("welcome " + account.UserName);
return true;
}
else
{
Console.WriteLine("invalid access");
return false;
}
}
}
return false;
}
class Account
{
public string UserName { get; set; }
public string Password { get; set; }
public int Group { get; set; }
}
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
public int DesiredGroup { get; set; }
}
class Answer
{
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
public string AcceptableLetter { get; set; }
}
static void Main(字符串[]args)
{
列表帐户列表=新列表();
accountsList.Add(新帐户{UserName=“jack”,Password=“111”,Group=1});
accountsList.Add(新帐户{UserName=“ibo”,Password=“121”,Group=2});
控制台。写入(“输入用户名>>”;
字符串username=Console.ReadLine();
控制台。写入(“输入密码>>”;
字符串密码=Console.ReadLine();
if(检查用户密码(帐户列表、用户名、密码))
{
列出问题=填写问题();
int结果=0;
for(int i=0;ix.UserName==UserName.Group)
{
继续;
}
Console.WriteLine();
Console.WriteLine(问题[i].问题文本);
Console.WriteLine(“-----------------------------------”);
书面回答(问题[i].回答列表);
字符串answers=Console.ReadLine();
for(int j=0;j class Answer
{
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
public string AcceptableLetter { get; set; }
}
List<Account> accountsList = new List<Account>();
accountsList.Add(new Account { UserName = "jack", Password = "111", Order = 1 });
accountsList.Add(new Account { UserName = "ibo", Password = "121", Order = 2 });
private static bool CheckUserPassword(List<Account> accountsList, string username, string password)
{
foreach (Account account in accountsList)
{
if (account.UserName == username)
{
if (account.Password == password)
{
Console.WriteLine("welcome " + account.UserName);
return true;
}
else
{
Console.WriteLine("invalid access");
return false;
}
}
}
return false;
}
private static List<Question> FillQuestions()
{
List<Question> questionList = new List<Question>();
List<Answer> answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "Ankara", IsCorrect = true, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Istambul", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Izmir", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Türkiyenin baskenti neresidir?", AnswersList = answerList });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "ismet", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Atatürk", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Ali", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Baskomutan kim?", AnswersList = answerList });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "2", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "4", IsCorrect = true, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "2 kere 2?", AnswersList = answerList });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1912", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "1914", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "1915", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "When did the world war 1 start?", AnswersList = answerList });
return questionList;
}
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
// added property
public int DesiredGroup { get; set; }
}
if (questions[i].DesiredGroup == accountsList.Find(x => x.UserName == username).Group)
{
continue;
}
static void Main(string[] args)
{
List<Account> accountsList = new List<Account>();
accountsList.Add(new Account { UserName = "jack", Password = "111", Group = 1 });
accountsList.Add(new Account { UserName = "ibo", Password = "121", Group = 2 });
Console.Write("enter username >> ");
string username = Console.ReadLine();
Console.Write("enter password >> ");
string password = Console.ReadLine();
if (CheckUserPassword(accountsList, username, password))
{
List<Question> questions = FillQuestions();
int result = 0;
for (int i = 0; i < questions.Count; i++)
{
if (questions[i].DesiredGroup == accountsList.Find(x => x.UserName == username).Group)
{
continue;
}
Console.WriteLine();
Console.WriteLine(questions[i].QuestionText);
Console.WriteLine("--------------------------");
WriteAnswers(questions[i].AnswersList);
string answers = Console.ReadLine();
for (int j = 0; j < questions[i].AnswersList.Count; j++)
{
if (questions[i].AnswersList[j].AcceptableLetter == answers)
{
if (questions[i].AnswersList[j].IsCorrect)
{
Console.WriteLine(questions[i].AnswersList[j].AcceptableLetter + " is correct");
result += 10;
}
else
{
Console.WriteLine(questions[i].AnswersList[j].AcceptableLetter + " is incorrect");
}
}
}
}
if (result < 15)
{
Console.WriteLine("failed");
}
else
{
Console.WriteLine("congrats");
}
}
Console.Read();
}
private static void WriteAnswers(List<Answer> answersList)
{
char[] alphabetLetters = { 'a', 'b', 'c' };
for (int i = 0; i < answersList.Count; i++)
{
Console.WriteLine(alphabetLetters[i] + ") " + answersList[i].AnswerText);
}
}
private static List<Question> FillQuestions()
{
List<Question> questionList = new List<Question>();
List<Answer> answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "Ankara", IsCorrect = true, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Istambul", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Izmir", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Türkiyenin baskenti neresidir?", AnswersList = answerList, DesiredGroup = 1 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "ismet", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "Atatürk", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "Ali", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "Baskomutan kim?", AnswersList = answerList, DesiredGroup = 1 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "2", IsCorrect = false, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "4", IsCorrect = true, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "2 kere 2?", AnswersList = answerList, DesiredGroup = 2 });
answerList = new List<Answer>();
answerList.Add(new Answer { AnswerText = "1912", IsCorrect = false, AcceptableLetter = "a" });
answerList.Add(new Answer { AnswerText = "1914", IsCorrect = true, AcceptableLetter = "b" });
answerList.Add(new Answer { AnswerText = "1915", IsCorrect = false, AcceptableLetter = "c" });
questionList.Add(new Question { QuestionText = "When did the world war 1 start?", AnswersList = answerList, DesiredGroup = 2 });
return questionList;
}
private static bool CheckUserPassword(List<Account> accountsList, string username, string password)
{
foreach (Account account in accountsList)
{
if (account.UserName == username)
{
if (account.Password == password)
{
Console.WriteLine("welcome " + account.UserName);
return true;
}
else
{
Console.WriteLine("invalid access");
return false;
}
}
}
return false;
}
class Account
{
public string UserName { get; set; }
public string Password { get; set; }
public int Group { get; set; }
}
class Question
{
public string QuestionText { get; set; }
public List<Answer> AnswersList { get; set; }
public int DesiredGroup { get; set; }
}
class Answer
{
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
public string AcceptableLetter { get; set; }
}
public class QuestionsByAccountProvider
{
Question[] GetQuestionsForAccount(Account account)
{
// Put your code for providing questions here.
}
}
var questionProvider = new QuestionsByAccountProvider();
var questions = questionProvider.GetQuestions(account);