Try this
for (int counter = 1; counter <= 10; counter++)
{
Console.WriteLine(counter*counter);
}
문제
I need to display the squares of the numbers 1-10 using a for loop. This is what I have so far. I don't know what I am missing. Any help would be much appreciated.
for (int counter = 1; counter <= 10; counter++)
{
Console.WriteLine(counter * counter);
}
Console.ReadLine();
해결책
Try this
for (int counter = 1; counter <= 10; counter++)
{
Console.WriteLine(counter*counter);
}
다른 팁
Have a look at your code
for (int counter = 1; counter <= 10; counter++)
{
if ((counter * counter) == 0) // this will never evaluate to true
{
Console.WriteLine(counter);
}
}
Since you are starting off with 1 your if condition is never true, so nothing would be printed
you just need to use counter * counter
printed in your for loop
or you can use Math.Pow(counter, 2.0)
to get your squares
For an integer counter
having any value other than 0
, counter * counter
will never evaluate to 0
.
if ((counter * counter) == 0) This will not satisfy for any value..Try if ((counter * counter) != 0) ..Try this..
Since you start with 1, that counter * counter can not be 0. So, having that in mind, here's the whole code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
Console.WriteLine(i * i);
}
}
}
I am sure that this was helpful.