im new to C# and my logic wont work
it keeps on displaying my else command
there is no errors
///////////////////////////////////////////////////////////////////
int age = 12;
if ((age <= 0) && (age >= 12))
{
Console.WriteLine("You are young");
}
else if ((age <= 13) && (age >= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age <= 18) && (age >= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age <= 51) && (age >= 120))
{
Console.WriteLine("You're Elderly");
}else
{
Console.Beep();
}
///////////////////////////////////////////////////////////////////
You just need to swap your conditions for every age range:
int age = 12;
if ((age >= 0) && (age <= 12))
{
Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
Console.WriteLine("You're Elderly");
}
else
{
Console.Beep();
}
int age = 12;
if ((age >= 0) && (age <= 12))
{
Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
Console.WriteLine("You're Elderly");
}else
{
Console.Beep();
}
There is nothing wrong with syntax but your logic
Take a look at all <= condition.
The conditions within the if statements aren't correct, try this:
int age = 12;
if ((age >= 0) && (age <= 12))
{
Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
Console.WriteLine("You're Elderly");
}
else
{
Console.Beep();
}
Related
Okay, I have a load of else if statements, is there a way to condense them into fewer lines of code? Like make one if statement for all of it? And what ways could I make my code more optimize and easier to read?
int x;
int y;
int time=0;
Random rng = new Random();
int hrand_num = rng.Next(-24000, 24000);
int vrand_num = rng.Next(-24000, 24000);
x = hrand_num;
y = vrand_num;
while (true)
{
ConsoleKey key = Console.ReadKey().Key;
if (key == ConsoleKey.UpArrow)
{
y=y+2000;
}
else if (key == ConsoleKey.DownArrow)
{
y=y-2000;
}
else if (key == ConsoleKey.LeftArrow)
{
x = x-1000;
}
else if (key == ConsoleKey.RightArrow)
{
x = x+1000;
}
// Circumnavigate Players Position.
// North and South
if (y >= 24001)
{
y = -24000;
}
else if (y <= -24001)
{
y = 24000;
}
//West and East
else if (x >= 24001)
{
x = -24000;
}
else if (x <= -24001)
{
x = 24000;
}
// Setting Time Zones
if (x >= -2000 && x <= 0 )
{
time = 0;
}
else if (x >=
1 && x <= 2000)
{
time = 1;
}
else if (x >= 2001 && x <=4000)
{
time = 2;
}
else if (x >= 4001 && x <= 6000)
{
time = 3;
}
else if (x >= 6001 && x <= 8000)
{
time = 4;
}
else if (x >= 8001 && x <= 10000)
{
time = 5;
}
else if (x >= 10001 && x <= 12000)
{
time = 6;
}
else if (x >= 12001 && x <= 14000)
{
time = 7;
}
else if (x >= 14001 && x <= 16000)
{
time = 8;
}
else if (x >= 16001 && x <= 18000)
{
time = 9;
}
else if (x >= 18001 && x <= 20000)
{
time = 10;
}
else if (x >= 20001 && x <= 22000)
{
time = 11;
}
else if (x >= 22001 && x <= 24000)
{
time = 12;
}
else if (x == -24000 && x <= -22001)
{
time = 13;
}
else if (x >= -22000 && x <= -20001 )
{
time = 14;
}
else if (x >= -20000 && x <= -18001)
{
time = 15;
}
else if (x >= -18000 && x <= -16001)
{
time = 16;
}
else if (x >= -16000 && x <= -14001)
{
time = 17;
}
else if (x >= -14000 && x <= -12001)
{
time = 18;
}
else if (x >= -12000 && x <= -10001)
{
time = 19;
}
else if (x >= -10000 && x <= -8001)
{
time = 20;
}
else if (x >= -8000 && x <= -6001)
{
time = 21;
}
else if (x >= -6000 && x <= -4001)
{
time = 22;
}
else if (x >= -4000 && x <= -2001)
{
time = 23;
}
Console.WriteLine($"X: {x,6} Y: {y,6} Time: {time,3}");
}
```
Assuming that you are using C# 8.0 you may have a look at the switch statement and switch expressions: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression (additionaly the patterns documentation is also helpful: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns)
So you could write something like:
switch (key)
{
case ConsoleKey.UpArrow:
y=y+2000;
break;
// [...]
}
time = x switch
{
>= -2000 and <= 0 => 0,
>= 1 and <= 2000 => 1
// [...]
};
I would suggest that you take advantage of:
a switch statement for interpreting the arrow key
comparing the switch statement to an if loop, you can think of the first case as being the if condition and the remaning cases as being the else if conditions
Math.Abs( ) and Math.Sign( ) for circumnavigating the player's position
Math.Abs( ) returns the absolute value of the variable; e.g. will Math.Abs(x) return 5 both for x = 5 and x = -5
Math.Sign( ) returns the sign of the value; if the value is a negative number, it returns -1; if it's a positive number, it returns 1; if it's neither (0), it returns 0. This helps us determine the wanted sign of the updated value.
a switch expression for setting the time
seeing as the time value alone is determined by x in the end, you can use a switch expression rather than a switch statement to determine its value. The switch expression says that you want to determine the value of time based on the value of x; and each following condition is compared to x (<= -22001 is computed as x <= -22001). If the condition evaluates to true, the provided value is set as the value of time (=> 13 then sets time = 13).
It could be implemented like this:
int x;
int y;
Random rng = new Random();
int hrand_num = rng.Next(-24000, 24000);
int vrand_num = rng.Next(-24000, 24000);
x = hrand_num;
y = vrand_num;
while (true)
{
switch (Console.ReadKey().Key)
{
case ConsoleKey.UpArrow:
y += 2000;
break;
case ConsoleKey.DownArrow:
y -= 2000;
break;
case ConsoleKey.LeftArrow:
x -= 1000;
break;
case ConsoleKey.RightArrow:
x += 1000;
break;
}
// Circumnavigate Players Position.
// North and South
if (Math.Abs(y) > 24000)
{
y = -(Math.Sign(y) * 24000);
}
//West and East
else if (Math.Abs(x) > 24000)
{
x = -(Math.Sign(x) * 24000);
}
// Setting Time Zones
var time = x switch
{
<= -22001 => 13,
<= -20001 => 14,
<= -18001 => 15,
<= -16001 => 16,
<= -14001 => 17,
<= -12001 => 18,
<= -10001 => 19,
<= -8001 => 20,
<= -6001 => 21,
<= -4001 => 22,
<= -2001 => 23,
<= 0 => 0,
<= 2000 => 1,
<= 4000 => 2,
<= 6000 => 3,
<= 8000 => 4,
<= 10000 => 5,
<= 12000 => 6,
<= 14000 => 7,
<= 16000 => 8,
<= 18000 => 9,
<= 20000 => 10,
<= 22000 => 11,
<= 24000 => 12,
_ => 0
};
Console.WriteLine($"X: {x,6} Y: {y,6} Time: {time,3}");
I would also suggest introducing some constants; particularily for the value 24000.
You can use the following to cover all time cases
var time = x <= 24000
? x / 2000 + 1;
: (24000 - x) / 2000 + 13;
I am trying to create a screen where the user would be able to write down their name with StringBuilder. The problems I have is with the functionality of backspace. I can remove all letters except the first letter that was pressed. Also, it seems like I can press whatever character and it would be submitted.
ConsoleKeyInfo cki = new ConsoleKeyInfo();
bool enterPressed = false;
StringBuilder name = new StringBuilder();
int temp = 61;
do
{
Console.SetCursorPosition(temp, 14);
cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter && name.Length > 0 && name.Length < 12)
{
enterPressed = true;
Console.SetCursorPosition(61, 18);
Console.Write(name);
}
else if ("qwertyuiopasdfghjklzxcvbnm".Contains(cki.KeyChar) && name.Length < 12)
{
name.Append(cki.KeyChar);
Console.Write(cki.KeyChar);
temp += 1;
}
else if(cki.Key == ConsoleKey.Backspace && name.Length > 0)
{
name.Remove(name.Length-1, 1);
Console.Write("\b \b");
}
} while (name.Length > 0 && !enterPressed);
You forgot to do temp--;
This code snippet should solve your problem
name.Remove(name.Length - 1, 1);
temp--;
Console.Write("\b \b");
You can try below code
console.Writeline("Enter Your Name");
string name= console.Readline();
and below is something for you to update if you don't want to use above solution.
if(cki.Key == ConsoleKey.Backspace && name.Length >= 0)
{
if (name.Length == 0)
{
name.Remove(0, 0);
}
else
{
name.Remove(name.Length - 1, 1);
}
temp--;
Console.Write(" ");
}
Basically what im doing is using NAudio to get peak info. I've made a set of 20 images that are going to represent the EQ. I've done some code to check the peak value and change the image in the picturebox in realtime with a timer. Though sometimes I see visual lag and I think the code is quite inefficient. Is there a more efficient code than this large chunk of ifs
private void EQ_TIMER_Tick(object sender, EventArgs e)
{
int vol = (int)((float)(device.AudioMeterInformation.MasterPeakValue * 100));
if ((vol > 0) && (vol < 5))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ5;
}
else if ((vol > 5) && (vol < 10))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ10;
}
else if ((vol > 10) && (vol < 15))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ15;
}
else if ((vol > 15) && (vol < 20))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ20;
}
else if ((vol > 20) && (vol < 25))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ25;
}
else if ((vol > 25) && (vol < 30))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ30;
}
else if ((vol > 30) && (vol < 35))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ35;
}
else if ((vol > 35) && (vol < 40))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ40;
}
else if ((vol > 40) && (vol < 45))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ45;
}
else if ((vol > 45) && (vol < 50))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ50;
}
else if ((vol > 50) && (vol < 55))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ55;
}
else if ((vol > 55) && (vol < 60))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ60;
}
else if ((vol > 60) && (vol < 65))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ65;
}
else if ((vol > 65) && (vol < 70))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ70;
}
else if ((vol > 70) && (vol < 75))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ75;
}
else if ((vol > 75) && (vol < 80))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ80;
}
else if ((vol > 80) && (vol < 85))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ85;
}
else if ((vol > 85) && (vol < 90))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ90;
}
else if ((vol > 90) && (vol < 95))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ95;
}
else if ((vol > 95) && (vol < 100))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ100;
}
else if (vol == 0)
{
WINGS_FRAME.Image = null;
}
}
Try something like this:
int vol = 38;
int imgnum = vol/5*5;
string imgname = string.Format("RES_EQ{0}", imgnum);
var image = Properties.Resources.ResourceManager.GetObject(imgname);
Integer division "rounds" the volume to a step number, then multiplying by 5 gives the actual number for the image.
I want to know performance differences between below two code lines. Which one is faster?
1.
anyList = Enumerable.Range(0, 1440).Select((n, i) =>
{
if ((i >= 480 && i < 790) || (i >= 1050 && i < 1170)
return 0;
else if ((i >= 790 && i < 1050)
return 1;
else
return 2;
}).ToList();
2.
for (int i = 0; i < 1440; i++)
{
if ((i >= 480 && i < 790) || (i >= 1050 && i < 1170)
anyLisy.Add(0);
else if ((i >= 790 && i < 1050)
anyLisy.Add(1);
else
anyLisy.Add(2);
}
Which one is faster, which one has much more allocated memory?
use StopWatch and make some benchmark something like this
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 1440; i++)
{
if ((i >= 480 && i < 790) || (i >= 1050 && i < 1170))
anyLisy.Add(0);
else if (i >= 790 && i < 1050)
anyLisy.Add(1);
else
anyLisy.Add(2);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
I want to ask if anyone knows how to limit the period allowed for a textbox. I'm using C# Visual Studio 2010.
My problem is I need to find validation codes that will ensure that only a single period is allowed for the textbox Middle Initial of the user. And if the user type another period, then the period will not be shown in the textbox. No error messages is needed. Example is the validation code that accept letters only. Here is my code for this example:
private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
{
byte num = Convert.ToByte(e.KeyChar);
if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122) || (num == 8) || (num == 32))
{
}
else if (num == 13)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
else
{
e.Handled = true;
}
}
I have the following codes currently for my txtboxMI:
private void txtMI_KeyPress(object sender, KeyPressEventArgs e)
{
byte num = Convert.ToByte(e.KeyChar);
if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122) || (num == 8) || (num == 32))
{
}
else if (num == 13)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
else
{
e.Handled = true;
}
}
Do you have to make it server-side?
You can use regular expression validator.
[a-zA-Z_-.] should give you what you want.
Try this:
var txt = (TextBox)sender;
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == 8 || e.KeyChar == 32) {
} else if (txt.Text.Contains('.') && e.KeyChar == '.') {
e.Handled = true;
} else if (e.KeyChar == '\t') {
e.Handled = true;
SendKeys.Send("{Tab}");
}