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 4 years ago.
Improve this question
i just started out with C# and im confused by a Compile Error.
This is the Code:
namespace Control_Flow_1
{
class Program
{
static void Main(string[] args)
{
string UserInput;
int Number;
Console.WriteLine("Bitte geben sie eine Nummer von 1-10 ein");
UserInput = Console.ReadLine();
Number = Convert.ToInt32(UserInput);
if (Number >= 1 && Number <= 10);
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
}
}
}
The } after the Console.WriteLine; seems to do the Compile Error for some Reason, how to fix it ?
Compile Error:
if (Number >= 1 && Number <= 10); get rid of the semi colon.
With the semi colon after the if you have
if ()
a block of code
else (with no corresponding if)
a block of code
The semi colon ends the if statement and so you have a "floating" else with no previous if
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 2 years ago.
Improve this question
Hi my c# is not what it used to be and have just come back after using java script for a while.
Essentially I am just trying to do a simple if statement using index of arrays but I receive the error message.
"Operator '||' cannot be applied to operands string and string"
How come this is not allowed as it essentially becoming a bool.
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", "I struggle to see this"};
int customResponseindex = rand.Next(0, 3);
string[] questionResponseArray = { "Yes", "No but not a problem", userCustomAnswerArray[customResponseindex] };
int questionResponseIndex = rand.Next(0, 3);
string userAnswer = questionResponseArray[questionResponseIndex];
if (userAnswer = questionResponseArray[0] || userAnswer = questionResponseArray[1])
{
}
Thanks for your help !!!!
userAnswer = questionResponseArray[0] is incorrect.
= is the assignment operator while == is the equality operator
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
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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.
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.
Improve this question
I need to print num after num(num-1) until zero
have to do it in recursion style.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is num?");
int num = int.Parse(Console.ReadLine());
downnums(num);
}
public static void downnums(int num)
{
if (num == 0)
Console.WriteLine("that all");
else
{
Console.WriteLine(downnums(num-1));
}
}
}
Thanks guys
Remember: void means your method doesn't return a value, so it cannot be assigned to other variable or passed to method as an argument like WriteLine method:
this is what you're looking for:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is num?");
int num = int.Parse(Console.ReadLine());
downnums(num);
}
public static void downnums(int num)
{
if (num == 0)
Console.WriteLine("that all");
else
{
Console.WriteLine(num);
downnums(num-1);
}
}
}
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
I'm trying to write an if statement and i'm having trouble with my variables. it states operator > can not be applied to type int and string. code located below. both variables are displaying a int.
if (e.CmsData.Skill.InQueueInRing > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
}
else if (e.CmsData.Skill.AgentsAvailable > "0")
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString(); }));
callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
}
else
{
callsWaitingData.Text = "0";
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
}
This error couldn't really get much more descriptive.
operator > can not be applied to type int and string
if (e.CmsData.Skill.InQueueInRing > "0")
int -----^ ^--- string
Change it to
if (e.CmsData.Skill.InQueueInRing > 0)
Then both sides of the boolean logic is an int.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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.
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.
Improve this question
Question: My else-statement is unreachable, what am i doing wrong?
very VERY new at programming and i'm trying to compare the type so for example people can't enter strings when i'm asking for integers.
My code is probably pretty bad, if i could get a header what to do and why the if-argument skips the else-part i'd be really happy!
Thanks!
class Program
{
static void Main(string[] args)
{
int integer = 0;
start:
Console.WriteLine("How old are you?: ");
int svar = int.Parse(Console.ReadLine());
Utility.CompareTypes(svar, integer);
if (true)
{
Console.WriteLine("Thanks");
}
else
{
Console.WriteLine("You have to enter a number!");
goto start;
}
}
}
class Utility
{
public static bool CompareTypes<T01, T02>(T01 type01, T02 type02)
{
return typeof(T01).Equals (typeof(T02));
}
}
:c
It's not really a question of code, but of logic...
if (true) // <--- this will ALWAYS be true
{
Console.WriteLine("Thanks");
}
else // <--- therefore this will NEVER happen
{
Console.WriteLine("You have to enter a number!");
goto start;
}
Since your else block can never possibly execute under any logical circumstance, the entire block of code can be simplified to:
Console.WriteLine("Thanks");
In order for the else block to execute, the condition checked in the if statement needs to be false. You're currently not checking any actual condition, just a hard-coded true value.
Perhaps you meant to use the result of the previous line of code? Something like this:
var typesAreSame = Utility.CompareTypes(svar, integer);
if (typesAreSame)
{
//...