我是第一年的學生,我們被要求做assigment,我不知道是否有任何其他方式來檢查用戶輸入,所以我不會做所有的,如果公司和其他各輸入後的或者是有什麼辦法可以循環並保存6個不同的答案或類似的東西.....C#如何使這個代碼更緊湊(如果,否則如果,否則)
static void Main(string[] args)
{
//var
string s0, s1, s2, s3, s4, s5; //subejects
string g0, g1, g2, g3, g4, g5;//grades
//input grades/subject
Console.Write("Enter Subject 1 : ");
s0 = Console.ReadLine();
Console.Write("Enter Grade 1 : ");
g0 = Console.ReadLine();
g0 = g0.ToUpperInvariant();
if (g0 == "H1")
g0 = "100";
else if (g0 == "H2")
g0 = "88";
else if (g0 == "H3")
g0 = "77";
else if (g0 == "H4")
g0 = "66";
else if (g0 == "H5" || g0 == "O1")
g0 = "56";
else if (g0 == "H6" || g0 == "O2")
g0 = "46";
else if (g0 == "H7" || g0 == "O3")
g0 = "37";
else if (g0 == "H8")
g0 = "0";
else if (g0 == "O4")
g0 = "28";
else if (g0 == "O5")
g0 = "20";
else if (g0 == "O6")
g0 = "12";
else if (g0 == "O7" || g0 == "O8")
g0 = "0";
else
Console.WriteLine("Wrong Grade Format!");
Console.Write("\nEnter Subject 2 : ");
s1 = Console.ReadLine();
Console.Write("Enter Grade 2 : ");
g1 = Console.ReadLine();
g1 = g1.ToUpperInvariant();
Console.Write("\nEnter Subject 3 : ");
s2 = Console.ReadLine();
Console.Write("Enter Grade 3 : ");
g2 = Console.ReadLine();
g2 = g2.ToUpperInvariant();
Console.Write("\nEnter Subject 4 : ");
s3 = Console.ReadLine();
Console.Write("Enter Grade 4 : ");
g3 = Console.ReadLine();
g3 = g3.ToUpperInvariant();
Console.Write("\nEnter Subject 5 : ");
s4 = Console.ReadLine();
Console.Write("Enter Grade 5 : ");
g4 = Console.ReadLine();
g4 = g4.ToUpperInvariant();
Console.Write("\nEnter Subject 6 : ");
s5 = Console.ReadLine();
Console.Write("Enter Grade 6 : ");
g5 = Console.ReadLine();
g5 = g5.ToUpperInvariant();
Console.Clear();
//collect information into array
for (int i = 0; i <= 5; i++)
{
string[] subjects = { s0, s1, s2, s3, s4, s5 };
string[] grades = { g0, g1, g2, g3, g4, g5 };
Console.WriteLine("{0,15}:{1,-15}", subjects[i],grades[i]);//output
}
}
}
請別生氣,如果IM問愚蠢的問題很簡單但我可以在網上找到的是更先進的方法....謝謝。
您可以使用'Dictionary'將輸入字符串映射到輸出字符串。那麼你只需要在一個地方初始化字典。 –
也可以使用'GetSubjectAndGrade(int num)'返回一個'元組'或類似的東西 –
Jonesopolis
您好, 我建議你把你的字符串放入一個枚舉中...... g0 ==「H1 「如果你需要改變它,真的很糟糕。然後我將這個代碼包裝到一個函數中。您的Console.Write()調用您可以在循環中執行 - 將值分配給數組g []。最好的關注和良好的外觀與你的研究 – TripleEEE