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");
}
Related
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 1 year ago.
Improve this question
for(int i = 1; i != amount+1; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Append(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Append(Convert.ToDecimal(Console.ReadLine()));
}
(This section is what is affected)
It gets the inputs from the user (I.E. it gets the names and grades, though it doesn't add to the array. I am new to C#, coming from python. Do you guys have any idea how I could fix this issue? TIA
Are you looking for something like this?
using System.Collections.Generic;
int amount = 5;
List<string> people = new List<string>();
List<decimal> grades = new List<decimal>();
for (int i = 0; i < amount; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Add(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Add(Convert.ToDecimal(Console.ReadLine()));
}
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
I am writing this function, however it gives me: The name 'binarySearchRecursive' does not exist in the current context. I dont understand why i am getting this message, the call is done in the scope of this method itself? Or do i misunderstand a fundamental part of c#?
public static int binarySearchRecusive<T>(T[] a, int low, int high, T v) where T : IComparable
{
if (low < high)
{
var middle = (low + high) / 2;
if (a[middle].CompareTo(v) == 0)
return middle;
if (a[middle].CompareTo(v) < 0)
return binarySearchRecursive(a, low, middle - 1, v);
else
return binarySearchRecursive(a, middle+1, high - 1, v);
}
return -1;
}
Your function goes by this name:
binarySearchRecusive
If you change it to:
binarySearchRecursive
it will work.
Typpo in binarySearchRecusive, instead, try binarySearchRecursive.
If it still don't work then try Program.binarySearchRecursive()
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am new to C#, and need a little help, i know in PHP that i can to something like this
$SelectedEvent="event selected";
if (i > 0)
{
$SelectedEvent = "event";
}
It replace a string easy, but in c# that is not working ok, here is example code i have that is not working
string SelectedEvent = "event selected";
if (i > 0)
{
SelectedEvent = "event";
}
This is not working like in PHP, i can not override the varible?
Here is my example code
EDITED
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
string SelectedEvent = "event";
}
}
You should not redeclare the variable twice but only modify its value:
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
SelectedEvent = "event";
}
// here you can use the SelectedEventVariable
// its value will be "event selected" on the first and second
// iterations and "event" on subsequent
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem, i am facing in this code is that first iteration of loop is good but in second iteration when i press y program gives error.
Error = Input string was not in a correct format.
Line of Error = my = int.Parse(Console.ReadLine());
static void Main(string[] args)
{
int a, my;
char again = 'y';
while ((again == 'y' || again=='Y'))
{
Console.Write("Enter the value for your number = ");
my = int.Parse(Console.ReadLine());
Random b = new Random();
a = b.Next(1, 6);
if (a == my)
{
Console.WriteLine("Congratulations");
}
else
{
Console.WriteLine("you Lost");
Console.WriteLine("My no is {0}.", a);
}
Console.Write("Again? Then press 'y' or 'Y' = ");
again = (char)Console.Read();
}
Console.ReadLine();
}
Change
again = (char)Console.Read();
to
again = (char)Console.ReadLine().First();
Console.Read Reads the next character from the standard input stream. To continue you press a key ('y' in this case) and press 'enter'. Which means you input 2 characters and read 1. Which one is read by next Console.Readline. So, you never get the actual string in this line
my = int.Parse(Console.ReadLine());
Rather you get the character you did not read previously. And this one can not be parsed to integer.
If you got Input string was not in a correct format in a Int32.Parse, try replace it for a Int32.TryParse
Int32.TryParse(Console.ReadLine(), out my);