要素の初期セットを設定せずに配列を宣言するにはどうすればよいですか? または、要素の初期セットを入力することは必須ですか? ここに私のコードがあります:
class Program
{
static void Main(string[] args)
{
string ans, uname, choice, input;
int temp;
Console.WriteLine("Hi! We're Adrianne and Marco, today is " + DateTime.Now + ", what's yours?");
uname = Console.ReadLine();
do
{
Console.WriteLine("Hello, " + uname + "! Please select a function:");
Console.WriteLine("1: Palindrome");
Console.WriteLine("2: Prime or Not Prime");
Console.WriteLine("3: Bubble Sort");
Console.WriteLine("4: Fibonacci");
choice = Console.ReadLine();
if (choice == "1")
{
Console.WriteLine("Enter any word or string:");
input = Console.ReadLine();
temp = Palindrome(input);
if (temp == 0)
Console.WriteLine(input + " is not a palindrome...");
else
Console.WriteLine(input + " is a palindrome!");
}
else if (choice == "2")
{
Console.WriteLine("Enter a number:");
input = Console.ReadLine();
temp = Prime(input);
if (temp == 0)
Console.WriteLine(input + " is prime");
else if (temp == 1)
Console.WriteLine(input + " is not prime");
else
Console.WriteLine(input + " is neither prime nor composite");
}
else if (choice == "3")
{
int h;
string tempo;
double[] inputs = { 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < 8; i++)
{
h = i + 1;
if (i == 0)
{
Console.WriteLine("Enter 1st number:");
tempo = Console.ReadLine();
}
else if (i == 1)
{
Console.WriteLine("Enter 2nd number:");
tempo = Console.ReadLine();
}
else if (i == 2)
{
Console.WriteLine("Enter 3rd number:");
tempo = Console.ReadLine();
}
else
{
Console.WriteLine("Enter " + h + "th number:");
tempo = Console.ReadLine();
}
inputs[i] = Convert.ToDouble(tempo);
}
bubbleSort(inputs);
Console.WriteLine(inputs[0] + " " + inputs[1] + " " + inputs[2] + " " + inputs[3] + " " + inputs[4] + " " + inputs[5] + " " + inputs[6] + " " + inputs[7]);
}
else if (choice == "4")
{
Console.WriteLine("Enter a whole number:");
input = Console.ReadLine();
int temp3 = Convert.ToInt16(input);
int[] fibNums = {0, 0, 0, 0, 0};
//for (int i = 0; i < fibNums.Length; i++)
//{
// Console.WriteLine(fibNums[i]);
//}
int temp4 = 0;
do
{
temp = fibSequence(temp4);
fibNums[temp4] = temp;
temp4++;
} while (temp <= temp3);
for (int i = 0; i < fibNums.Length; i++)
{
Console.WriteLine(fibNums[i]);
}
}
Console.WriteLine("There, all finished! Try again? (Y/N)");
ans = Console.ReadLine();
} while (ans == "Y");
Console.WriteLine("Thank you, come again!");
Console.Read();
}
エラーはこの部分「int[] fibNums = {0, 0, 0, 0, 0};」にあります。私のコードでは、入力に応じて、配列は実際には無限の数の要素を持つ可能性があります。しかし、配列を宣言する方法がわからないため、要素の数は初期化する要素の数にのみ制限されます。