Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to make some sort of mastermind game in the console where the computer generates a 3 digit code and you have to guess it. The computers also tells you which digit you get right after you take a guess. How would i best do this? I don't know a way to "chop up" input so that i can make the computer check it against the digit it has.
example:
computer generates random code: 123
you guess: 321
computer : -*- (the star indicates which digit you got right)
I have an idea about how i'd indicate what digits are right or not, but i don't know how to separate the input into parts
Convert the computer generated random number into a string. The user input is given as string anyway.
The string type has an indexer allowing you to access single characters
string s = "abc";
char a = s[0];
char b = s[1];
char c = s[2];
for (int i = 0; i < s.Length; i++) {
Console.WriteLine($"s[{i}] = '{s[i]}'");
}
You write char literals as
char ch = 'x';
using single quotes.
Example:
string userInput = Console.ReadLine();
int code = SomehowGenerateRandomCode();
string codeString = code.ToString();
// Let's assume that both numbers have 3 digits.
for (int i = 0; i < 3; i++) {
if(userInput[i] == codeString[i]) {
// digits are equal
} else {
// digits are different
}
}
To split an int to its digits you can convert to string an split like:
int i = 123;
string myInt = i.ToString();
List<int> digits =new List<int>();
foreach (char c in myInt)
{
digits.Add(int.Parse(c.ToString());
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Currently, I am trying to convert coffee script from there https://github.com/slang800/instagram-id-to-url-segment to C# but I am unable to convert because I have less exp in coffee script. What I need to do is simple but I am unable to guess the algo. If id is 1038059720608660215 then value should be 5n7dDmhTr3. The complete code of algo is available at github.
Try this
static void Main(string[] args)
{
string[] convert = {
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","0","1","2","3","4","5","6","7",
"8","9","-","_"
};
Int64 input = 1038059720608660215;
string output = "";
for (int i = 9; i >= 0; i--)
{
long digit = (input >> (6 * i)) & 0x3F;
output += convert[digit];
}
Console.WriteLine(output);
Console.ReadLine();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c # i'm trying to add two different integer name, is it possible
int variablename;
for(int i=0;i<10;i++)
{
Objectname.variablename+i
}
No, you cant. C# is strongly typed programming language. What you can do, is to use Dictionary for example, like this:
Dictionary<string, int> names = new Dictionary<string,int>();
for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}
var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
If I understand you correctly you want to get the name of your variable and then change the name for each instance based on the iteration, thus you want to add a numeric value to the name. You can achieve this as follows:
string testVariable = "";
int i = 1;
// Get the name of your variable. This will result in "testVariable" as value.
string nameOfTestVariable = nameof(testVariable);
// Now add your integer to the name, but add it as a string.
nameOfTestVariable += i.ToString();
//Result, value of nameOfTestVariable: "testVariable1"
To get the datatype of your variable instead:
int a = 0;
int b = 1;
string dataTypesOfVariables = a.GetType().Name;
dataTypesOfVariables += b.GetType().Name;
//Result, value of dataTypesOfVariables: "Int32Int32"
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Stuck for hours on this task. I really appreciate help, it is not shown in the literature how this is to be done. I can create the vector but after that I'm totally blocked.
Create a string vector with five elements. The user will then enter 5 names via a for loop. The program then writes these names via another for loop.
Please type out the code so I can understand. Without solving this first task I can't do the other ones.
Here's my futile attempt so far
int [] namn = new int [5];
for (int i = 0; i < 5; i++);
{
Console.Write("Ange fem namn");
string str = Console.ReadLine();
int names = Convert.ToInt32(str);
}
It seems that the main (and the most difficult error to find) is ; after for loop
for (int i = 0; i < 5; i++);
this loop does nothing five times.
//DONE: "string[]" - Create a STRING vector with five elements...
string [] namn = new string [5];
//DONE: namn.Length - no magic numbers (5)
for (int i = 0; i < namn.Length; i++) // !!! no ";" !!!
{
Console.Write("Ange fem namn");
//DONE: put into array, not to a local variable
namn[i] = Console.ReadLine();
}
// Print out the names
foreach(var item in namn)
Console.WriteLine(item);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want an auto-numbering class in c# which would generate numbers of 8 digit length in the following format i.e. 1A2B3C4D..one number followed by one letter.Any suggestions??
Pseudocode for generating such string:
String result = "";
for ( int i = 0; i < 8 ; i++)
{
if ( i % 2 == 0)
{
// random(a,b) returns random value between or equal to a-b
result.append(random(0,9).toString());
}
else
{
result.append(random(65,90).toChar()); // Generating a random value between 65-90 (A-Z in ascii)
}
}
Edit:
Or as Sayse suggested:
String result = "";
for (int i = 0; i< 4; i++)
{
result.append(random(0,9).toString());
result.append(random(65-90).toChar());
}