English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Given:
an int variable k ,
an int array currentMembers that has been declared and initialized,
an int variable memberID that has been initialized, and
an boolean variable isAMember ,
write code that assigns true to isAMember if the value of memberID can be found in currentMembers , and that assigns false to isAMember otherwise. Use only k , currentMembers , memberID , and isAMember .

My Code:
for (k=0; k {
if (currentMembers[k] == memberID)
isAMember = true;
else
isAMember = false;
}

why is this not working? I need correction help from you guys, thanks

2006-10-18 10:00:12 · 5 answers · asked by ? 1 in Computers & Internet Programming & Design

5 answers

first of all, remove the "-1" from the condition - this way you are leaving out the last element. Also, as said above, stop as soon as you find the right guy, to avoid it being reset back to false next time.

Here is a fancy way to do that:
for(k=0,isAMember=false;
k !isAMember;
isAMember=
(currentMembers[k++]==
memberID));

note the semicolon in the end - this loop does not need any body, all the work is done in the for() statement (this actually is supposed to be a sinle line - I just had to break it, because stupid yahoo keeps truncating it - it'll work this way too, just not as much fun as having solved the problem with just a single line of code :-))

2006-10-18 11:02:37 · answer #1 · answered by n0body 4 · 0 0

Here you go. You need to initalize the k variable as an integer. See the following code:
public class CheckMember{

public static void main(String[] args){
int[]currentMembers = {555, 235, 458, 777, 458};

int memberID = 777;
boolean isAMember = false;

for (int k=0; k {
if (currentMembers[k] == memberID){
isAMember = true;
}
}

System.out.println(isAMember);
}
}

2006-10-18 10:57:33 · answer #2 · answered by Mark M 2 · 1 0

in all probability the right individuals way is sturdy one , yet once you prefer to appreciate the coolest judgment , a million. assign the 1st factor as max 2. loop in the process the climate and evaluate the array components with the max fee 3. if got here across greater suitable than the present max factor replace it . 4. print the max fee , that's the optimal fee interior the array

2016-10-02 10:39:23 · answer #3 · answered by ? 4 · 0 0

It should exit the loop as soon as it finds a current member otherwise it continues to search and may set isamember to false if the next one is not a member.

should be
for (k=0; k

2006-10-18 10:51:05 · answer #4 · answered by tru_story 4 · 0 0

You might try this (currentMembers.length-1) instead of currentMembers.length-1
not sure but it is worth a shot

2006-10-18 10:09:09 · answer #5 · answered by JohnDoe 1 · 0 0

fedest.com, questions and answers