Why is my TryParse code not working in C#? [closed] - 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 2 years ago.
Improve this question
There is some problem in my while loop, and I don't know what the problem is:
double d = 0;
while (!Double.TryParse(Console.ReadLine(), d <= 20 && d >= 1, out d))
{
Console.WriteLine("The number is incorrect, please write in again");
}

double d = 0;
while (!Double.TryParse(Console.ReadLine(), out d) && d <= 20 && d >= 1)
{
Console.WriteLine("The number is incorrect, please write in again");
}
Like this. You accidentally passed the d <= 20 && d >= 1 as an argument for the TryParse, instead of using it as an additional condition for the while loop.

Because you're misunderstanding tryparse, d is set after try parse, then you can compare it, you have put the boundaries check in the arguments of try parse:
while (!Double.TryParse(Console.ReadLine(), out var d) || d <= 20 && d>=1)
{ //you first check if you can parse it, and after parse is true, check if value is in your boundaries
Console.WriteLine("The number is incorrect, please write in again");
}

Related

Why is the for-loop choosing the wrong IF statement path? [closed]

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 3 years ago.
Improve this question
So I am doing an online coding challenge and have come across this issue that has me stumped:
This is my code:
static void Main(String[] args)
{
int noOfRows = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < noOfRows; i++)
{
string odds = "";
string evens = "";
//get the input word from console
string word = Console.ReadLine();
for (int j = 0; j < word.Length; j++)
{
//if the string's current char is even-indexed...
if (word[j] % 2 == 0)
{
evens += word[j];
}
//if the string's current char is odd-indexed...
else if (word[j] % 2 != 0)
{
odds += word[j];
}
}
//print a line with the evens + odds
Console.WriteLine(evens + " " + odds);
}
}
Essentially, the question wants me to get the string from the console line and print the even-indexed characters (starting from index=0) on the left, followed by a space, and then the odd-indexed characters.
So when I try the word 'Hacker', I should see the line printed as "Hce akr". When I debugged it, I saw the code successfully put the letter 'H' on the left (because it is index=0, thus even), and put the letter 'a' on the right (odd index). But then when it got to the letter 'c', instead of going through the first IF path (even index), it skips it and goes to the odd index path, and places it on the right hand side?
The funny thing is when I try the word 'Rank' it works fine and prints the correct statement: "Ra nk", yet other words do not.
Its just bizarre that I'm getting different results.
What am I missing?
word[j] is a character in your string; j is the index you want to check the evenness of.
if (j%2) should provide the correct path. You're using if( word[j] %2) which is doing modular arithmetic on a character, not an index. Most likely using modulo on the ASCII value. Hope this helps.
you want to check if the index is even,yet you compare word[j] % 2 == 0 which is not an index.
what you should do:
if(j % 2 == 0){
}

Why does for-loop show nothing in cmd [closed]

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 6 years ago.
Improve this question
I have problem with for loop in C#.
I have the following code, in main method:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i=0;i>20;i++)
{
if(i%2==0)
{
Console.WriteLine("{0},", i);
}
}
Console.ReadLine();
}
}
}
The for loop does not execute. Why?
Change condition, > to <:
...
for (int i = 0; i < 20; i++) // i < 20, not i > 20
...
In your original code you assign int i = 0 then check i > 20 get false and do not enter the loop at all
The following statement in your code:
for (int i = 0; i > 20; i++)
First assigns 0 to i then evaluates the condition i > 20 which is wrong, so the for block can not be executed.
Other people have pointed out the fact that the test should actually be "i < 20" rather than "i > 20) but just to make one other point: if all you're trying to do is write out all the even numbers, you can actually increment "i" by 2 every time - that way you never have to test to see if it's even (just write "i += 2" instead of "i++").

CS1513 error } expected [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I keep getting a CS1513 error on the following code in visual studios writing in C#
{
class NotChineseZodiac
{
static void Main(string[] args)
{
String YearBorn;
int YearBornInt;
Console.WriteLine("What was the year of your birth?");
YearBorn = Console.ReadLine();
YearBornInt = Convert.ToInt32(YearBorn);
Console.WriteLine("You are" + (DateTime.Now.Year - YearBornInt));
if ((DateTime.Now.Year - YearBornInt) < 18);
Console.WriteLine("You Shall Not PASS");
else
Console.WriteLine("Please Proceed");
Console.ReadLine(); // last enter key
Does anyone see my error??
you have terminated this line with a semicolon:
if ((DateTime.Now.Year - YearBornInt) < 18);
correctly it would be:
if ((DateTime.Now.Year - YearBornInt) < 18)
Console.WriteLine("You Shall Not PASS");
else
Console.WriteLine("Please Proceed");
or for better readability
if ((DateTime.Now.Year - YearBornInt) < 18)
{
Console.WriteLine("You Shall Not PASS");
}
else
{
Console.WriteLine("Please Proceed");
}

Cannot assign to 'writeline' because it is a 'method group' [closed]

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 7 years ago.
Improve this question
The below code is giving me error "Cannot assign to 'writeline' because it is a 'method group;" at every Console.Writeline statement. I am trying to find out why but I could not find anything to point me in right direction. Any help appreciated.
{
class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine = "Fizzbuzz" ;
}
else if (i % 3 == 0)
{
Console.WriteLine = "Fizz";
}
else
(i % 5 == 0)
{
Console.WriteLine = "Buzz";
}
System.Console.ReadLine();
}
}
}
You need to set the string in parenthesis:
Console.WriteLine("This string goes to console.");
You tried to assign the method with a value, that is not possible. That works only with properties and fields.

operator && cannot be applied to operands type int and int [closed]

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 8 years ago.
Improve this question
Today I started to learn random and I got a problem that I cannot figure out, can someone tell me what did I do wrong?
int playerNum1, playerNum2;
Random rnd = new Random();
int num1 = rnd.Next(1, 11);
int num2 = rnd.Next(1, 11);
Console.WriteLine("Insert 2 numbers");
playerNum1 = int.Parse(Console.ReadLine());
playerNum2 = int.Parse(Console.ReadLine());
if ((num1=playerNum1) && (num1=playerNum2) && (num2=playerNum2) && (num2 =playerNum1))
Comparison in C# is done with == not with =
if (num1==playerNum1 && num1==playerNum2 && num2==playerNum2 && num2 == playerNum1)

Categories

Resources