我需要一些幫助來解決我在 Visual Studio 中收到的 C# 錯誤。他們宣告不能將字串轉換為 int 或不能將 int 轉換為字符。請問有人能幫我解決這個問題嗎?
Console.WriteLine(" The largest value in the array is ", numberArray, maximumIndex, "located at array index" maximumIndex);
//Loop through the two arrays and print the integer from integer array followed by the corresponding value from the string array
Console.WriteLine(" The numbers were: ");
for (int i = 0; i < numberArray.Length; i )
Console.WriteLine(numberArray[i], " is ", stringArray[i]); **Getting error for "numberArray"-Cannot convert 'int' to 'char; Getting error for "is"-Cannot convert string to int; Getting error for "stringArray"-Cannot convert string to int**
uj5u.com熱心網友回復:
C# 方法
盡管 (,) 運算子可能會在 Python 或其他一些語言中使用,但 (,) 運算子在 Console.[method] 引數中并不是真正的 C# 語法。
解決方案
您可以使用這些方法中的任何一種,這完全取決于偏好。我將從我最喜歡的方法到其他可能的方法對它們進行排名:
Console.WriteLine($"{numberArray[i]} is {stringArray[i]}");
// Example output: 1 is 2
在我看來,這是最干凈的解決方案。但另一個是使用Concatination
Console.WriteLine(numberArray[i] " is " stringArray[i]);
// Example output: 1 is 2
希望這有幫助。存在更多處理此主題的方法,但目前這兩種方法值得告訴您,想不出任何更舒適的版本,但我確定它們存在。
在結束這個答案之前,我只想指出您在 for 回圈中缺少括號。我不確定 C# 是否允許這樣做,因為我從來都不是 C 語法的忠實粉絲,但如果您遇到另一個錯誤,請嘗試添加括號,因為我假設該問題遲早會出現。
uj5u.com熱心網友回復:
您需要在 console.writeline 中執行 而不是 a ,
uj5u.com熱心網友回復:
我修改了你的代碼如下;
public static void Main(string[] args)
{
int[] numberArray = {1, 5, 10};
var maximumIndex = 2;
Console.WriteLine("The largest value in the array is {0} {1} located at array index{2}", string.Join(",", numberArray), maximumIndex, maximumIndex);
}
輸出:陣列中的最大值是 1,5,10 2 位于陣列 index2
參考內插字串
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/351962.html
