I am trying to make a method that takes 2 arguments and returns true or false if the parameters of an array are within a certain value.
The programs is a game, were 2 players are given 3 piles of sticks, each pile has a different number of sticks, the player to remove the last stick wins. (so there are only 3 indexes in the array, 0,1,2)
here is the code, the part i am stuck on is at the end of the code, there are notes to explain how the method should work. thanks in advance
Mansukhdeep Thind wrote: . . .
Always write the logic in plain English or your native language first. Something like:
. . .
Far better way to do it:-reference Are you supposed to loop through the array? If so, you would require different pseudo‑code.
Joined: Apr 28, 2013
The descriptions were not actually written by myself, they are there to help me that is why they are quite long.
So how do i write an 'If' statement to ask if the value inside each index is within it's parameters. for example if the index value is 5, then if i subtracted between 1 - 5 it would be true, if i tried to subtract 8 it would be false
The method has 2 arguments, it;s first asking if the array length is valid from 0 to 2 and then the value inside each index is in range, i hope that makes sense.
battousai liang wrote:So how do i write an 'If' statement to ask if the value inside each index is within it's parameters.
If it's a small, fixed number of parameters (probably no more than 3 or 4), you can do it as:
Of course "in range" has to be replaced by an appropriate test, such as > 0. And if you have more than a single comparison to make, such as "between two numbers," then you'll have to mention each index multiple times.
You can do it for more than that if it's a fixed number, but the code gets unwieldy. For a large or variables number of items to test, you'd use an array or collection, and a loop. If any one of the items fails its test, you set a flag to indicate that, and break out of the loop.