我是C#的初學者,我正在研究這個包含4個方法(包括Main())的程序。 GetValues()允許用戶輸入數組,FindAverage()計算數組的平均值,現在我想創建第三個應該顯示結果的方法'Show()'。 當我在Main()中顯示平均值時,我得到了這個工作,但是當我在Show()中執行時,沒有任何反應。在測試它時,我讓Show()顯示一條簡單的文本行「消息」,但是當我運行它時。看起來像程序跳過Show()並直接進入Main()。你們中的任何一個人可以向我解釋發生了什麼,我該怎麼做才能讓Show()顯示它的內容?爲什麼程序跳過方法?
謝謝你們!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testscores
{
class Program
{
private static int GetValues()
{
string inValue;
int[] score = new int[5];
int total = 0;
for (int i = 0; i < score.Length; i++)
{
Console.Write("Enter Value {0}: ", i + 1);
inValue = Console.ReadLine();
score[i] = Convert.ToInt32(inValue);
}
for (int i = 0; i < score.Length; i++)
{
total += score[i];
}
return total;
}
//FIND AVERAGE
private static double FindAverage()
{
double total = GetValues();
double average = total/5.0;
return average ;
}
//Show
static void Show()
{
Console.WriteLine("message");
return;
}
static void Main()
{
double avg = FindAverage();
Console.WriteLine("The Average is :" + avg);
Console.ReadKey();
}
}
}
你不打電話展()的任何地方...... –
你可以肯定它不會「跳過」如果你調用它。我沒有看到你調用這個方法的代碼,所以你顯示的代碼是不相關的。請顯示您所描述的代碼以及您認爲問題出在哪裏。 – CodeCaster
@Mitch也許我輸入的速度不如你快? – CodeCaster