[英]How to declare an array in C#?
如何在不設置初始元素集的情況下聲明數組? 還是必須輸入初始元素集? 這是我的代碼:
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};”。 在我的代碼中,數組實際上可以具有無限數量的元素,具體取決於輸入。 但是由於我不知道如何聲明數組,因此元素的數量僅限於初始化它的元素的數量。
我認為List<int>
會更適合您的目的。 它將根據需要自動擴展。
List<int> fibNums = new List<int> {0, 0, 0, 0, 0};
如何在不提供數組初始化程序的情況下聲明數組類型的局部變量並對其進行初始化?
您已經知道如何聲明數組類型的局部變量並使用初始化程序對其進行初始化:
double[] inputs = { 0, 0, 0, 0, 0, 0, 0, 0 };
這種語法-使用數組初始化器且周圍沒有其他內容-僅在局部變量或字段聲明中是合法的。
您可能還不知道使用初始化器的其他語法。 這些在表達式合法的任何地方都是合法的,而不僅僅是在初始化器中:
double[] inputs = new double[] { 0, 0, 0, 0, 0, 0, 0, 0 };
double[] inputs = new double[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
當您不能說或不想說類型時,此語法很有用。 但是,您需要確保正確使用元素的類型:
double[] inputs = new[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
注意,我在那里使用0.0
而不是0
。 當您說new[]
,編譯器會嘗試從初始化程序中推斷出數組元素的類型,在原始情況下,它會推導int[]
,而不是double[]
。
您詢問了如何避免使用初始化程序語法。 你說:
double[] inputs = new double[8];
它將自動初始化為零。
正如其他人指出的那樣,使用List<double>
或其他某種收集類型可能會更好。
如何在不設置初始元素集的情況下聲明數組?
使用System.Collections.Generic.List
而不是數組。
如何實例化列表:
var fibNums = new List<int>();
然后,您可以根據需要簡單地添加數字:
int yourNumber = 1;
fibNums.Add(yourNumber);
而且,如果您需要一個整數數組,則仍然可以使用列表的“ ToArray”功能:
fibNums.ToArray();
如果要創建具有特定數量元素的數組,請使用new
運算符:
int[] fibNums = new int[temp3];
這將創建一個包含temp3
元素的數組,每個元素都初始化為默認值,對於int
和其他數字類型,該值為0
。
順便說一句,像temp3
這樣調用變量是一種不好的做法,請改用描述性名稱。
解:
首先:您需要聲明一個數組
int[] fibNums = new int[temp3+1];
我們使用temp3 + 1,因為我們從0開始
然后我們將循環條件更改為
} while (temp < temp3);
最終的代碼是:
int[] fibNums = new int[temp3+1];
//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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.