//create the new object for cars
Cars s1 = new Cars("Toyota", 2005, 500000, "White", "good");//Car1 Ob
Cars s2 = new Cars("Honda", 2004, 550000, "Black", "fine");//Car2 Ob
Cars s3 = new Cars("Nissen", 2012, 490000, "Yellow", "best");//Car3 Ob
Cars s4 = new Cars("Suzuki", 2012, 390000, "Blue", "fine");//Car4 Ob
Cars s5 = new Cars("BMW", 2012, 1000000, "Green", "Good");//Car5 Ob
//Create list to add objects into the memory
List<Cars> list1 = new List<Cars>();
list1.Add(s1);list1.Add(s2);list1.Add(s3);list1.Add(s4);list1.Add(s5);
//cars info which has the lowest price
double lowest_price = 0;
foreach(Cars a in list1){
if(a.price <= lowest_price){
lowest_price = a.price;
Console.WriteLine(a.price);
}
}//end of loop
Đó là mã mà tôi đang cố gắng in ra thông tin xe hơi có giá thấp nhất. Nhưng không có gì được in ra.Cách tìm giá trị thấp nhất từ danh sách?
Không của 'một .price' là ít hơn 0. 'a.price <= Lowest_price' luôn là false. –
Ngoài ra, bạn đang in 'a.price' - bạn không muốn in' giá_giá thấp nhất'? – Bridge
Oh .. đúng !! nó luôn luôn là sai !! Cảm ơn!! – user2042721