everyone. For starters this is my first C# code as a college freshmen(Still a noob here), I apologize in advance in my English. So basically I've been trying to solve this for 5 hours already, I'm real bad at this and this is my first ever post for help here.
So this code's goal is:
1.input an integer 1-2. 1 is draw and 2 is exit
2.once the draw starts, the code will get a random index inside that name array
3.then the random picked element should be removed in that name array in order to get new random element.
4.the process repeats until non of the four names appear no more when printed.
using System;
class Program
{
static void Main(string[] args)
{
bool isTrue = true;
while (isTrue)
{
//user choices
Console.WriteLine("[1]Draw\n[2]Exit\n");
//the input
Console.Write("Enter action: ");
int choices = Convert.ToInt32(Console.ReadLine());
//array of names
string[] names = {"Mr. Albert", "Mr. Joshua", "Mr. Bernard", "Mr. Raphael"};
//for random generating of names
Random rand = new Random();
int randNames = rand.Next(names.Length);
//Draw
if(choices == 1){
Console.WriteLine("Congrats " names[randNames]);
names[randNames] = " ";
for(int i=0; i < names.Length; i ){
Console.WriteLine(names[i]);
}
}
else if(choices == 2){
isTrue = false;
}
}
}
}
}
So my main problem is here in this code with the given array of names
string[] names = {"Mr. Albert", "Mr. Joshua", "Mr. Bernard", "Mr. Raphael"};
In this part of the code, which I was having a hard time figuring out
if(choices == 1){
Console.WriteLine("Congrats " names[randNames]);
names[randNames] = " ";
for(int i=0; i < names.Length; i ){
Console.WriteLine(names[i]);
}
What I did here is whenever I draw the picked numbers here should turn into " "
that or just make it look empty as I draw and draw three times. I tried to search this problem too, but no luck.
the results would look like this:
[1]Draw
[2]Exit
Enter action: 1
Congrats Mr. Albert
Mr. Joshua
Mr. Bernard
Mr. Raphael
[1]Draw
[2]Reset
[3]Exit
Enter action: 1
Congrats Mr. Joshua
Mr. Albert
Mr. Bernard
Mr. Raphael
[1]Draw
[2]Reset
[3]Exit
Enter action:
You could see in the result that they are not there, but in the next draw they still exist. Hope someone could help me, Thanks for the help in advance.
CodePudding user response:
Declare your array before while loop:
string[] names = {"Mr. Albert", "Mr. Joshua", "Mr. Bernard", "Mr. Raphael"};
while (isTrue)
{
//do your stuff
}
Right now, you edit your array and on the next iteration declare new array, which is unedited.