【发布时间】:2021-12-25 18:52:48
【问题描述】:
我有这个代码,这是一个简单的游戏,你选择多少个问题和你想要多少个字符,程序会生成一个随机的字符和数字字符串,但是我如何在每个问题中选择一个随机字符并询问用户有多少例如,这个字符被重复的时间 char A 在此字符串中重复了多少次:iasfhAjfalkjA 答案 = 2 次strong>
我不知道我是否应该把整个代码,但我认为这是我的代码有帮助
{
class MainClass
{
public static void Main(string[] args)
{
Random randomgen = new Random();
int a; string Sa; int b; string sb;
int maxanswer = 0;
int m = 0;
string question;
string sss;
string quit = "QUIT";
string useranswer;
string answer ;
int numofquestions;
int numofquestionsleft;
int numofcorrect = 0;
int numoffalse = 0;
//ASKING THE USER FOR THE MAX NUMBER OF QUESTIONS WANT TO BE ASKED
Console.Write("Max Question : ");
numofquestions = Convert.ToInt32(Console.ReadLine());
numofquestionsleft = numofquestions;
//ASKING THE USER TO PUT A NUMBER BETWEEN 3 AND 100 THAT WILL HELP GENERATE A RANDOM string
Console.WriteLine("enter a value between 3 and 100");
Sa = Console.ReadLine(); a = Int32.Parse(Sa);
//here is the random generating part
String sarffsa = "A1a2B3b4C5c6D7d8E9eFfGgHhIiKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
while (numofquestionsleft > 0 && m <= numofquestions + 1)
{
int length = a;
String random = "";
for (int i = 0; i < length; i++)
{
int alphapet = randomgen.Next(42);
random = random + sarffsa.ElementAt(alphapet);
}
Console.WriteLine("How many time the symbol has been repeted in the follwing characters: {0}", random);
answer = Console.ReadLine();
// adding the question and the user answer and the right answer to an array
questionlist[m] = random;
useranswer = Convert.ToString(Console.ReadLine());
useranswerlist[m] = answer;
rightanswerslist[m] = answer;
Console.WriteLine("--------------------------------------------------------");
// IF USER TYPE QUIT THAT WILL SKIP THE QUESTION AND ADD IT TO WRONG ANSWERS LIST
if (answer== quit)
{
numofquestionsleft--;
;
continue;
}
numofquestionsleft--;
//LOOP
m++;
}
while (1 < 2)
{
Console.WriteLine(@"TO GET THE NUMBER OF THE RIGHT ANSWERS PRESS 1
TO GET THE NUMBER OF THE WRONG ANSWERS PRESS 2
TO GET THE OPERATION WITH THE MAX NUMBERS OF RIGHT ANSWERS PRESS 3
TO GET THE OPERATION WITH THE MAX NUMBERS OF FALSE ANSWERS PRESS 4
TO VIEW ALL THE QUESTIONS AND YOUR ANSWERS AND CORRECT ANSWERS TYPE 5
TO EXIT TYPE EXIT
");
sb = Console.ReadLine();
b = Int32.Parse(sb);
switch (b)
{
// the number of right answers
case 1:
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("You have = " + numofcorrect + " Right answers");
Console.WriteLine("-----------------------------------------------");
break;
case 2:
// the number of wrong answers
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("You have = " + numoffalse + " Wrong answers");
Console.WriteLine("-----------------------------------------------");
break;
// Making the rightoperationlist into a group in order and count to show the most frequent array used
case 3:
var result = (from operation in rightoperationlist
group operation by operation into og
orderby og.Count() descending
select og.Key).FirstOrDefault();
Console.WriteLine("RIGHT ANSWER OPERATION = " + result);
break;
case 4:
// Making the Wrongoperationlist into a group in order and count to show the most frequent array used
var result02 = (from operation in wrongoperationlist
group operation by operation into og
orderby og.Count() descending
select og.Key).FirstOrDefault();
Console.WriteLine("WRONG ANSWER OPERATIONS = " + result02);
break;
//showing the questions and user answer and the right answer arrays
case 5:
Console.WriteLine("QUESTIONS ANSWERS RIGHTANSWERS");
Console.WriteLine("------------------------------------------------------------------------");
for (int z = 0; z < numofquestions; z++)
{
Console.WriteLine(questionlist[z] + " " + useranswerlist[z] + " " + rightanswerslist[z]);
}
Console.WriteLine("------------------------------------------------------------------------");
break;
default:
break;
}
}
Console.ReadLine();
}
}
}
【问题讨论】:
-
如果您想知道为什么在字符串中从未看到 Q 或 Z,请查看您的
randomgen.Next调用。这是作业题吗? -
您的问题到底是什么?我读到这个“我如何在每个问题中选择一个随机字符并询问用户这个字符重复了多少次”。您是否难以选择随机角色?这个字符是否需要包含在随机字符串中,或者您是否接受可能为零的答案(如果您的字符串不够长,很可能为零)。那么你问题的第二部分是什么?您需要确定实际答案吗?