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 am really knew to programming so please be nice haha. Excuse the "Noob" Question, I'm just experimenting right now. Can anyone give me tips on what is causing
CS1513 C# } expected
My code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string country = "USA";
Console.WriteLine("Hello, What country are you from?");
string countryName = Convert.ToString(Console.ReadLine());
if (countryName == country) ;
{
Console.WriteLine("You are Eligable for the competition ! :-) ");
}
else {
Console.WriteLine("You are not Eligable, Sorry!!");
}
}
}
}
I'm trying to give an answer based on the country of the user basically.
Remove the ; from if (countryName == country) ;
The ; is a statement terminator. See why do some lines not have semicolon in C#?
You put a ; after your if statement.
It should be if (countryName == country) instead of if (countryName == country) ;
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 4 months ago.
Improve this question
I just finished a C# totorial and thought it would be a cool idea to get the most popular type of computers using lists. I was able to get it to work exept the only the most recent element will be added to the list.
namespace computerDatabase
{
class program
{
static void Main()
{
while (true)
{
List<string> computerName = new List<string>();
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
computerName.Add(cName);
if (cName == "list")
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
else
{
computerName.Add(cName.ToLower());
}
}
}
}
}
As pointed out by Jon, you re-initializing your list for every iteration, nullifying any input added to the list. You also do not need to specify a capacity for the list in the constructor as it will grow automatically. I've corrected the relevant parts for you:
List<string> computerName = new List<string>();
while (true)
{
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
if (cName != "list")
computerName.Add(cName.ToLower());
else
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
}
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've tried adding a ) where it says to, but that adds more problems. The error I get states:
Assets/LevelComplete.cs(12,64): error CS1026: ) expected.
What I'm trying to do here is use the if statement to find out if the Active Scene is in the specific range. Also, for a little more context, All I need is a range, and not the code to pick a number in that range if that helps.
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LEvelComplete : MonoBehavior
{
public void LoadNextLevel()
{
int Active12 = Range(1, 3);
int Load34 = UnityEngine.Random.Range(3, 5);
if (SceneManager.GetActiveScene().buildIndex = Active12
{
SceneManager.LoadScene(Load34);
}
}
}
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;
public class question_63947858_script_error : MonoBehaviour
{
public void LoadNextLevel()
{
int Active12 = Random.Range(1, 3);
int Load34 = Random.Range(3, 5);
if (SceneManager.GetActiveScene().buildIndex == Active12)
{
SceneManager.LoadScene(Load34);
}
}
}
Things wrong:
MonoBehaviour was spelled wrong
System was clashing with UnityEngine namespace. So disambiguated with Random = UnityEngine.Random
No parentheses after the if statement
= changed to == in the if statement
"find out if the Active Scene is in the specific range."
...
var scene = SceneManager.GetActiveScene();
if (scene.buildIndex >= 1 && scene.buildIndex <= 3)
... or if you prefer Linq:
if (Active12.Contains(SceneManager.GetActiveScene().buildIndex))
See also How to elegantly check if a number is within a range?
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 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)
{
//...
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.
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
My C# code keeps returnng me a wrong conversion result. As far as i know, the conversion formula is correct, but it keeps displaying wrong results.
e.g: 70°F gives me 12,777777 °C. Can you check my code out ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Temperature
{
class Program
{
class Temperature
{
public double temp;
public void Convert(double value)
{
double tEmp;
tEmp = (value - 32) / 1.8;
Console.WriteLine("The temperature in °C is :" + tEmp);
Console.ReadKey();
}
}
static void Main(string[] args)
{
double f;
Temperature c = new Temperature();
f = Console.Read();
c.Convert(f);
}
}
}
your problem is
f = Console.Read();
It just reads the first character, not your entire line of input. Try
f = Convert.ToDouble(Console.ReadLine());
Here's a good answer on the difference between Console.Read vs. Console.ReadLine()
Console.Read() returns the ordinal value of the next character in the input stream.
The first character in your input stream is '7' which has an ordinal value of 0x0037. Represented as decimal this is 55 and (55-32)/1.8 is 12.7777.
You should be using Console.ReadLine() rather than Console.Read().