102k views
5 votes
In JAVA,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 a 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.

User Bdeonovic
by
6.1k points

1 Answer

2 votes

Answer:

The code to this question can be given as:

Code:

int k; //declare integer variable k

//declared array and initialized array

int currentMembers[]={1,2,3,4,5,9,6,7};

//declared integer variable memberID.

int memberID=9;

//declared boolean variable isAMember

boolean isAMember;

isAMember=false;

//assign value

for (k=0; k<nMembers; k++)

//loop

if (currentMembers[k] == memberID) //check condition

{

isAMember=true;

//change value to true.

break;

//break loop

}

Explanation:

In the above code firstly we declare the integer variables that name is already given in the question that is k and memberID in this variable we assign value. Then we declare an array variable currentMembers and initialized values in array. Then we declare a Boolean variable isAMember. This variable is used to assign only true or false value. Then we declare a for loop. loop is used to check all array elements.In this loop, we use the if condition in the if block if the array element matches with memberID. It breaks the condition and end of the loop.

User Gangadhar Gandi
by
5.6k points