J
jgbka
Guest
Hello,
I am new to C# and am having hard time figuring out how to fix my code below to not have to ask for input twice if the first user input is in fact an integer. Do I need to declare some kind of static first in order to call it again from within the else statement? Should I use the threading class instead of what I have here to be able to pass user input to multiple threads? Thank you for any advice.
//C# Program to Generate Fibonacci Series
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace fibonaci
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of elements: ");
string input = Console.ReadLine();
int num = -1;
if (!int.TryParse(input, out num))
{
Console.WriteLine("Not an integer");
}
else
Console.Write("Enter the number of elements: ");
int n1 = 0, n2 = 1, n3, i, number;
number = int.Parse(Console.ReadLine());
Console.Write(n1 + " " + n2 + " "); //printing 0 and 1
for (i = 2; i < number; ++i) //loop starts from 2 because 0 and 1 are already printed
{
n3 = n1 + n2;
Console.Write(n3 + " ");
n1 = n2;
n2 = n3;
}
Console.ReadLine();
}
}
}
Continue reading...
I am new to C# and am having hard time figuring out how to fix my code below to not have to ask for input twice if the first user input is in fact an integer. Do I need to declare some kind of static first in order to call it again from within the else statement? Should I use the threading class instead of what I have here to be able to pass user input to multiple threads? Thank you for any advice.
//C# Program to Generate Fibonacci Series
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace fibonaci
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of elements: ");
string input = Console.ReadLine();
int num = -1;
if (!int.TryParse(input, out num))
{
Console.WriteLine("Not an integer");
}
else
Console.Write("Enter the number of elements: ");
int n1 = 0, n2 = 1, n3, i, number;
number = int.Parse(Console.ReadLine());
Console.Write(n1 + " " + n2 + " "); //printing 0 and 1
for (i = 2; i < number; ++i) //loop starts from 2 because 0 and 1 are already printed
{
n3 = n1 + n2;
Console.Write(n3 + " ");
n1 = n2;
n2 = n3;
}
Console.ReadLine();
}
}
}
Continue reading...