C# Case statement not entering [closed] - c#

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 8 years ago.
Improve this question
I am currently working my way through a Wrox C# book. However, following one of the tutorials that displays a Mandelbrot set, I am able to execute my program without error, however nothing is being displayed in the console. I am putting this down to an incorrect use of the switch I am using. Could anyone point me in the right direction?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch03Ex06
{
class Program
{
static void Main(string[] args)
{
double realCoord, imagCoord;
double realTemp, imagTemp, realTemp2, arg;
int iterations;
for (imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)
{
for (realCoord = -0.6; realCoord <= 1.77; realCoord += 0.03)
{
iterations = 0;
realTemp = realCoord;
imagTemp = imagCoord;
arg = (realCoord * realCoord) + (imagCoord * imagCoord);
while ((arg < 4) && (iterations < 40));
{
realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord;
imagTemp = (2 * realTemp * realTemp) - imagCoord;
arg = (realTemp * realTemp) + (imagTemp * imagTemp);
iterations += 1;
}
switch (iterations % 4)
{
case 0:
Console.Write(".");
break;
case 1:
Console.Write("o");
break;
case 2:
Console.Write("O");
break;
case 3:
Console.Write("#");
break;
}
}
Console.Write("\n");
}
Console.ReadKey();
}
}
}

i guess the program runs forever, because of this line
while ((arg < 4) && (iterations < 40));
the ; at the end closes this while loop without entering the next block

You have a typo:
while ((arg < 4) && (iterations < 40));
; must be removed

This is your problem, remove the semicolon
while ((arg < 4) && (iterations < 40));

Related

No output for loop program [closed]

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
The scope is to output the numbers from 1 to (input number) with the multiples of 3 being a * each. E.g. for 8 as input: 1,2,*,4,5,*,7,8. I tried to do this with a for loop but it has no output for some reason.
using System;
using System.Collections.Generic;
namespace HelpPleaseStackOverFlow
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int x;
for (x = 1; x <= number; )
if (x % 3 == 0)
{
Console.WriteLine("*");
}
else;
{
Console.WriteLine(x);
x = x++;
}
}
}
}
I tried converting the x variable (integer) to a string and then making it the * that should be printed out, in the first half of the IF statement and the last part with the ELSE basically the same.
I also tried to make the for statement like this:
for (x = 1; x <= number; x++)
with the x++ at the end but this just game me the result of ,,8. With an input of 7; 8 should not be part of this and I have no idea why it put 8 as an output.
most of the erroros are that you are lacking some {} and ()
String input = Console.ReadLine();
int number;
int.TryParse(input, out number);
for (int x = 1; x <= number; ++x)
{
if ((x % 3) == 0)
{
Console.WriteLine("*");
}
else
{
Console.WriteLine(x);
}
}
Console.ReadLine();
Hope it helps.

Unreachable code detected in simple method with only one return [closed]

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 6 months ago.
Improve this question
I know there's a bunch of related questions but I only have one line returning from this method and can't see why the return line would be unreachable:
public List<int> GetMiddleLaneForAI(Room r)
{
int laneCount = GetLaneCount(true);
List<int> matches = new List<int>();
for(int x = 0; 0 < 5; x++)
{
if(aiCardFrames[x].deployedCard == null)
{
if (laneCount < 3)
{
matches.Add(x);
} else
{
if (x > 0 && x < 4)
{
if (aiCardFrames[x - 1] != null && aiCardFrames[x - 1] != null) matches.Add(x);
}
}
}
}
return matches.Count > 0 ? matches : null; // unreachable code detected
}
Look closely at this line:
for(int x = 0; 0 < 5; x++)
When does this for loop exit? ;-)

C# Random.Next(min. max) return value less than min [closed]

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 6 years ago.
Improve this question
Does anyone have any idea why does this piece of C# code return x = 0 and y = 0 (ocassionally) :
public void NewPos()
{
int x = 0;
int y = 0;
while (lstPosition.Where(z => z.Position.X == x && z.Position.Y == y).Count() != 0) {
x = new Random().Next(4, 20) * 10;
y = new Random().Next(4, 20) * 10;
}
NewPos.X = x;
NewPos.Y = y;
Console.WriteLine(x + " - " + y );
}
You're not ever getting inside the while loop, although we can't tell what lstPosition is set to with the code you've provided. The where clause must be returning an empty set.
There's no way Random.Next(int, int) is returning zero in this situation.
Presumbably, you want to initalize x and y to a non-zero value.
You, probably, want something like this:
// Do not recreate random
// (otherwise you're going to have a badly skewed values);
// static instance is the simplest but not thread safe solution
private static Random s_Generator = new Random();
public void NewPos() {
// Just Any, not Where + Count
if (lstPosition.Any(z => z.Position.X == x && z.Position.Y == y)) {
// If we have such a point, resample it
NewPos.X = s_Generator.Next(4, 20) * 10;
NewPos.Y = s_Generator.Next(4, 20) * 10;
// Debug purpose only
Console.WriteLine(x + " - " + y );
}
}

How does sendInput() function work in C#? [closed]

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 7 years ago.
Improve this question
I'm doing a project using key stimulation and I'm using two ways to display a key character on the screen. First I use Clipboard but using Clipboard sometimes is inconvenient so I try to use SendInput() to display the key character when user press any button on the keyboard. I follow the code tutorial but I didn't know how does the SendInput() function work ( I mean when I invoke SendInput() what will it work step by step?). Does anyone have experience about my case? Please explain clearly for me.
Thanks all!
private void SendUnicode(string _text)
{
if (IntPtr.Size == 4)
{
ViKey.INPUT[] input = new ViKey.INPUT[_text.Length * 2];
for (int i = 0; i < _text.Length; i++)
{
input[i * 2].type = 1;
input[i * 2].ki.wVk = 0;
input[i * 2].ki.wScan = (short)_text[i];
input[i * 2].ki.dwFlags = 4;
input[i * 2 + 1] = input[i * 2];
input[i * 2 + 1].ki.dwFlags = 6;
}
ViKey.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
return;
}
else
{
ViKey.INPUT64[] inputx64 = new ViKey.INPUT64[_text.Length * 2];
for (int i = 0; i < _text.Length; i++)
{
inputx64[i * 2].type = 1;
inputx64[i * 2].ki.wVk = 0;
inputx64[i * 2].ki.wScan = (short)_text[i];
inputx64[i * 2].ki.dwFlags = 4;
inputx64[i * 2 + 1] = inputx64[i * 2];
inputx64[i * 2 + 1].ki.dwFlags = 6;
}
ViKey.SendInput((uint)inputx64.Length, inputx64, Marshal.SizeOf(inputx64[0]));
}
}
All relevant information can be found in the documentation about the function.
The function returns the number of events that it successfully
inserted into the keyboard or mouse input stream. If the function
returns zero, the input was already blocked by another thread.
So how it works is that it inserts the events in the INPUT structures (which are fed to the function through the parameters) serially into the keyboard or mouse input stream and then returns the number of those events that are successfully inserted..

Taking average value every 2 seconds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have a problem with arrays or something missed in these text..
my program works every 500ms and i want to read first 4 double values and take average of these values and then get next 4 double values and so on... i write something about this and can you pls look on this??
if (u_dcbus_pv_act[i] > 0 && i != 0)
{
u_dcbus_pv = u_dcbus_pv_act[i];
p_dcbus_pv = p_dcbus_pv_act[i];
}
if (i >= 3)
{
for (int j = 0; j < 4; j++)
{
total_u += u_dcbus_pv;
total_p += p_dcbus_pv;
}
average_u = total_u / 4;
average_p = total_p / 4;
u_dcbus_target = average_u;
p_dcbus_pv_avg = average_p;
}
from what I understand of your description, I would do it something like this:
/* add current samples to totals */
total_u += u_dcbus_pv_act[i];
total_p += p_dcbus_pv_act[i];
/* every fourth tick, calc average and reset totals */
if (i % 4 == 0)
{
average_u = total_u / 4;
average_p = total_p / 4;
total_u = 0;
total_p = 0;
}
u_dcbus_target = average_u;
p_dcbus_pv_avg = average_p;
i++;

Categories