• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with processing array

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

edited



 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to that ranch battousai.

Please use code tags when pasting code:



Always write the logic in plain English or your native language first. Something like:


Also, is this the whole class?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
battousai liang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic