标签云

微信群

扫码加入我们

WeChat QR Code

In this code, I am generating values for 2 different arrays. The value inside one array is dependant on the values inside the other array. The way it works is that in the array next_month_initial, the value is dependant on the corresponding value in the water_deficit array. However, the water_deficit array value is dependant on the previous (not corresponding) value in the next_month_initial array. Hence why the first value for the water_deficit array is calculated independently, as no previous value exists in the next_month_initial array. I hope this is clear enough to understand.The code may be slightly confusing but content wise it is correct, the calculations are correct. There is no error message shown however the program is unable to correctly calculate all of the values in the array. When I print the array, instead of seeing an array with the correct values listed it says "Playground execution failed". I have no idea why this is happening, as i can see it this code should work.var rainfall = [38,94,142,149,236,305,202,82,139,222,178,103]let max_h2Ostore = 150let carry_forward = 150var evap_transpiration: [Int] = []var water_deficit: [Int] = []var next_month_initial: [Int] = []// Generating values for water_deficit array//The first values is generated differently to the remaining valueswater_deficit[0] = rainfall[0] + carry_forward - evap_transpiration[0]for i in 0...11 {var x = iif water_deficit[i] <= 0 {next_month_initial.append(0)} else if water_deficit[i] >= max_h2Ostore {next_month_initial.append(max_h2Ostore)} else {next_month_initial.append(water_deficit[i])}; if i != 11 {x++water_deficit.append(next_month_initial[i] + rainfall[x] - evap_transpiration[x])}}println(water_deficit)


You haven't initialized the arrays, AFAICT. Oh, and wtf is going on with this line: }; if i != 11 {

2019年04月18日33分46秒

This is a serious case needing a Code Review... Your Swift is (sorry) horrible

2019年04月19日33分46秒

Kametrixom, you're right, this could use a Code Review, but only if the author's code is working.

2019年04月18日33分46秒

I am not surprised, I am very new to programming and swift any ideas on how to improve it?

2019年04月18日33分46秒

Quill Yes of course

2019年04月18日33分46秒

I have changed the code before the loop to your suggestion and this has solved the problem! So this was the issue all along, it had nothing to do with the loops. Im very new to programming so its no surprise my code is as you say horrible, any suggestions/ideas on what i should do to improve it? My code works 100% as it should now though and provides me with the outputs I am looking for. Thanks for the answer.Both the arrays u mentioned had been declared and filled previously in the program, sorry for not mentioning this in the question.

2019年04月19日33分46秒