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);
}
}
}
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 3 years ago.
Improve this question
I want to know how to use a global variable - which I already instantiated it as a public integer type - in a method later.
Here's my code so far:
public int money = 500000;
//other variables
//...some code in between
public static void UpdateResources (int cost, int airRate, int waterRate, int foodRate, int energyRate, int maintenanceRate, int happinessRate)
{
// \/ Problem here
if (money < cost)
{
//uncheck box
}
else
{
//implement input variables with other external variables
}
}
Remove "static" keyword from your method, static method cannot access instance variables. Static method is something belong to the type itself, while your instance variable is not. another option is to put the "money" as static, but than all your instances going to use the same "money" which is probably not what you aiming for.
public void updateResources (int cost, int airRate, int waterRate, int foodRate, int energyRate, int maintenanceRate, int happinessRate)
{
// v- No more Problem here :)
if (money < cost)
{
//uncheck box
}
else
{
//implement input variables with other external variables
}
}
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
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' ve got error on this code. Its said "Not all code paths return Value"
Please hel me fix this. Thanks
public static double subtotal;
public static void Main(String[] args)
{
subtotal = 15.00;
Console.WriteLine($"Subtotal : {subtotal}");
double total = subtotal + CalculateTax(7);
Console.WriteLine($"Total : {total}");
}
public static double CalculateTax(double taxRate)
{
double tax = subtotal * taxRate;
Console.WriteLine($"Tax: {tax}");
}
try this
public static double CalculateTax(double taxRate)
{
double tax = subtotal * taxRate;
Console.WriteLine($"Tax: {tax}");
return tax;
}
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)
{
//...