No output for loop program [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 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.

Related

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 );
}
}

c# home works can't solve [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 8 years ago.
Improve this question
I have to make console program that asks the user to enter number, then the program has to make square of stars (*) equal to the number that the user input.
Console.WriteLine("enter number and press ENTER");
int userNumber = int.Parse(Console.ReadLine());
int rowNumber = 0;
int lineNumber = 0;
int counter = 0;
while(counter < userNumber) {
Console.Write("*");
while(lineNumber < userNumber) {
Console.WriteLine("*");
lineNumber++;
}
counter++;
}
Console.WriteLine();
example:
user input :5
*****
* *
* *
* *
*****
you can do the following:
explanation for the code:
Square is formed of 2 equal sides, so in order to draw the square using *, you will think of it like a matrix
you need a loop to draw the rows and another loop to draw the columns, and both loop has upper limit which is the number entered by the user.
the rule of drawing is like this i will put star if i am in the first row or the last one or in the first column or last, so by addressing this i used the if statement ( i==0 || i== number-1 || j==0 || j== number -1) where i is the row and j is the column, and if this condition is not satisfied, print space
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Please enter a number:");
var number=Convert.ToInt32(Console.ReadLine());
for(int i=0; i < number; i++)
{
for(int j=0; j < number; j++)
{
if(i==0 || i == number-1 || j==0 || j == number-1)
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
}
}
here a working DEMO
hope this will help you

My C# array only sums and averages the last user input [closed]

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
I built a C# program in which the user is prompted to enter 10 different values and then the program is supposed to sum and average these values and print the sum and average. The problem I am having is that my program is only capturing the last value entered...please help!
namespace ConsoleApplication4
{
class Program
{
const int count = 10;
static void Input(double[] numbers, int num)
{
for (int i = 0; i <= 9; i++)
{
Console.Write("Enter integer {0}: ", i + 1);
numbers[num] = Convert.ToDouble(Console.ReadLine());
}
}
static void Average(double[] numbers, int num)
{
double sum = 0;
double avg = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
avg = sum / numbers.Length;
Console.WriteLine("The sum of the inputs is {0} and the average is {1}", sum, avg);
}
static void Main(string[] args)
{
double[] numbers = new double[count];
for (int num = 0; num < 1; num++)
{
Input(numbers, num);
Average(numbers, num);
Console.WriteLine("Press the Enter Key");
Console.ReadLine();
}
}
}
}
}
In you Input method, you're assigning user input to numbers[num] instead of numbers[i].
If you look at your Input() method, you're always storing in the num index in your for loop, but num doesn't change in the loop. You should be using numbers[i] instead of numbers[num].
for (int i = 0; i <= 9; i++)
{
Console.Write("Enter integer {0}: ", i + 1);
numbers[num] = Convert.ToDouble(Console.ReadLine());
That should be
numbers[i] = Convert.ToDouble(Console.ReadLine());

C# Case statement not entering [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 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));

Categories