ตรวจสอบโซลูชันสําหรับการดําเนินการทางคณิตศาสตร์ที่ส่งออกเป็นการทดสอบชนิดตัวเลขที่เฉพาะเจาะจง
โค้ดต่อไปนี้เป็นหนึ่งในวิธีแก้ไขปัญหาที่เป็นไปได้สําหรับการทดสอบจากหน่วยก่อนหน้า
int value1 = 11;
decimal value2 = 6.2m;
float value3 = 4.3f;
// The Convert class is best for converting the fractional decimal numbers into whole integer numbers
// Convert.ToInt32() rounds up the way you would expect.
int result1 = Convert.ToInt32(value1 / value2);
Console.WriteLine($"Divide value1 by value2, display the result as an int: {result1}");
decimal result2 = value2 / (decimal)value3;
Console.WriteLine($"Divide value2 by value3, display the result as a decimal: {result2}");
float result3 = value3 / value1;
Console.WriteLine($"Divide value3 by value1, display the result as a float: {result3}");
รหัสนี้เป็นเพียง "โซลูชันที่เป็นไปได้อย่างหนึ่ง" เนื่องจากมีหลายวิธีในการแก้ปัญหานี้ โซลูชันใช้การคัดเลือกมาก (และการเรียกเพื่อแปลง) อย่างไรก็ตาม อาจเป็นไปได้ว่าวิธีอื่นใช้งานได้ดีเท่ากัน เพียงตรวจสอบให้แน่ใจว่าผลลัพธ์ของคุณตรงกับเอาต์พุตต่อไปนี้
Divide value1 by value2, display the result as an int: 2
Divide value2 by value3, display the result as a decimal: 1.4418604651162790697674418605
Divide value3 by value1, display the result as a float: 0.3909091
ถ้าคุณประสบความสําเร็จ ขอแสดงความยินดี! ดําเนินการต่อไปที่การตรวจสอบความรู้ในหน่วยถัดไป
สําคัญ
หากคุณประสบปัญหาในการดําเนินการการทดสอบนี้ให้เสร็จสมบูรณ์ บางทีคุณควรตรวจสอบหน่วยก่อนหน้านี้ก่อนที่คุณจะดําเนินการต่อ