Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The while loop
public static bool Authentication()
{
bool isAuthenticated = false;
int count = 4;
while (isAuthenticated = false && count > 0)
{
Console.WriteLine("Please insert your 4 digit PIN");
string pin = Console.ReadLine();
if (pin == "1704")
{
isAuthenticated = true;
return isAuthenticated;
}
else
{
count--;
Console.WriteLine($"Wrong PIN: {count} Input remained ");
}
}
return isAuthenticated;
}
I haven't been able to understand, when the debugger goes to the while loop, it does not execute it. It may be the condition of the while that does not fit the logic.
I believe you are using the = as an assigning operator in the condition of the loop. You could try !isAuthenticated instead of isAuthenticated == false and see if that works
while (!isAuthenticate && count > 0)
I believe you were assigning false to isAuthenticate, so the condition could never be true.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Console.WriteLine("Would you like to go first or second?");
string firstSecond = Console.ReadLine().ToUpper();
if (firstSecond == "FIRST")
{
Console.WriteLine("1");
}
else if (firstSecond == "SECOND")
{
Console.WriteLine("2");
}
else
{
string input = "x";
bool first = input == "FIRST";
bool second = input == "SECOND";
do
{
Console.WriteLine("Please say first or second:");
input = Console.ReadLine().ToUpper();
}
while (!(first | second));
}
The do while loop keeps on going even if I type first or second, I tried changing how I state the boolean but nothing seems to work. I am new so I am might some silly mistakes. How do I fix this?
You must put first and second condition into do while loop.
string input;
bool first;
bool second;
do {
Console.WriteLine("Please say first or second:");
input = Console.ReadLine().ToUpper();
first = input == "FIRST";
second = input == "SECOND";
}
while (!(first || second));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So this should be super simple , perhaps indexOf is not the way to go
var incoming = "MDd"; // incoming data
List<string> cities = new List<string>();
cities.Add("MD");
cities.Add("Mumbai");
cities.Add("Berlin");
cities.Add("Istanbul");
if(cities.IndexOf(incoming) != 1 )
{
Console.WriteLine("found");
}
else
{
Console.WriteLine("not found");
}
I am seeing "found" with linqpad output , whether it is correct "MD" or "MDd" why? and what do I change to fix this?
From the documentation of IndexOf:
Return Value The zero-based index position of value if that string is
found, or -1 if it is not. If value is String.Empty, the return value
is 0.
When value is not found in the string, it returns -1, otherwise it returns index position, which is greater than or equal to 0. So either check if index is not -1 or check if it >= 0. I personally prefer the latter one:
// if (cities.IndexOf(incoming) != -1)
if (cities.IndexOf(incoming) >= 0)
{
Console.WriteLine("found");
}
This is a typo error. You want to check for -1 rather than 1 in your if statement:
if(cities.IndexOf(incoming) != -1)
{
Console.WriteLine("found");
}
else
{
Console.WriteLine("not found");
}
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 6 years ago.
Improve this question
i want my if loop to do nothing when the conditions that i gave it are there
i'm new to C# and winform so i searched the internet but didnt find an answer that seems to work and right now i have no idea what to do.
,Mo
screenshot of the loop
If I understand your question correctly, you want to "cancel" all operations in the current method, right? You can use return; to do that:
if(value2 == null) return;
There is just one other thing wrong with your code: value2 will never be null.
decimal value2;
if(!decimal.TryParse(result[1], out value2)) return;
should work a lot better ;)
I can't see any loop in the code provided. You want to change Parse to TryParse and use return in order to return from the method (== do nothing):
public void button14_Click(Object sender, EventArgs e) {
string[] result = input1.Text.Split(Oprator);
//TODO: it may appear, that you want TryParse here as well
decimal value1 = decimal.Parse(result[0]);
decimal value2;
// If you have too few items, and thus you have no "value2" - do nothing
if (result.Length < 2)
return;
// Try parse result[1] to decimal; if parse fails (e.g. result[1] == "zzz") - do nothing
if (!decimal.TryParse(result[1], out value2))
return;
// you have value1, value2, Oprator; put required logic here
switch (Oprator) {
...
}
}
Have you thought about a "While" loop?
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}
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'm having some trouble turning the following code into a for loop. The purpose of the code is to check if the string has both a letter and a number in it.
else if (!string.Any(Char.IsLetter) || !string.Any(Char.IsDigit))
{
return false;
}
Any ideas?
Do you mean something like this?
bool anyLetter = false;
bool anyDigit = false;
foreach(var ch in str)
{
if(char.IsLetter(ch)) anyLetter = true;
if(char.IsDigit(ch)) anyDigit = true;
if(anyLetter && anyDigit) break;
}
return anyLetter || anyDigit;
Note that if this string should contain at least one digit and one letter, you need to use && instead of ||
Since Selman22 seems to have already answered the question, another solution I found is I guess you could also use RegEx:
letterCount = Regex.Matches(yourstring,#"[a-zA-Z]").Count;
numberCount = Regex.Matches(yourstring,#"\d").Count;
return letterCount != 0 && numberCount != 0;
Did you mean a loop for a set of strings?
var src = new List<string>{"A4C", "D3F", "G7I"};
var allHaveCharAndDigit = src.TrueForAll(s => s.Any(c => char.IsLetter(c)) && s.Any(c => char.IsDigit(c)));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My program contains a textbox.
I need to check if it gets only numbers, then print.
int num;
if (this.Tree.GetType() == Main.TestInt.GetType())
{
if (int.TryParse(this.label.Text,out num) == true) // i tried without the == before
{
this.Tree.SetInfo(int.Parse(this.TextBox.Text));
base.label.Text = base.TextBox.Text;
}
else
{
base.TextBox.Text = "";
MessageBox.Show("Only Numbers Allowed", "Error");
}
}
The problem is, for some reason it always returns true, and goes to the
this.Tree.SetInfo(int.Parse(this.TextBox.Text));
Why is it happening?
2 changes:
int num;
if (this.Tree.GetType() == Main.TestInt.GetType())
{
if (int.TryParse(this.TextBox.Text,out num)) //1, you were parsing label.Text
{
this.Tree.SetInfo(num); //2, don't bother parsing it twice!
base.label.Text = base.TextBox.Text;
}
else
{
base.TextBox.Text = "";
MessageBox.Show("Only Numbers Allowed", "Error");
}
}
Probably you want to check the value of TextBox not the Label. So it would be this.TextBox.Text instead of this.Label.Text
if (int.TryParse(this.TextBox.Text,out num))
{
this.Tree.SetInfo(this.TextBox.Text);
base.label.Text = base.TextBox.Text;
}
else
{
base.TextBox.Text = string.Empty;
MessageBox.Show("Only Numbers Allowed", "Error");
}