java - If statement return loop -
just declaimer:i beginner in java.
write method named numunique takes 3 integers parameters , returns number of unique integers among three. example, call numunique(18, 3, 4) should return 3 because parameters have 3 different values. contrast, call numunique(6, 7, 6) return 2 because there 2 unique numbers among 3 parameters: 6 , 7.
public int numunique(int x,int y,int z){ if(x==y||y==z){ } return 1; else if(x!=y||y!=z){ } return 2; }
i confused relationship of if , return.i put return inside if statement.but dont understand why generate me error message.if fulfil,i return in loop.why wrong.but on other hand,the println statement can put inside loops.
another issue,because question,i tried attempt using if else too.but first condition if , return it.so after placed else if after first return,it gives me error again.
i appreciate explain me , alter codes on own.please dont give me full codes.thank you.
edited* way,i read through comments , understand it.this codes work out on own(:
public static int numunique(int x, int y, int z) { if(x==y && y==z){ return 1; }else if(x==y && y!=z || y==z && z!=x || x==z && y!=z ){ return 2; } return 3; }
first, return outside if
statements, should inside. because if write:
if(something) { } return 1;
then 1 returned because if
empty. if want return 1
if(something)
write inside if
body:
if(something) { return 1; }
second, logic not good. need check:
- if x == y == z they're equal.
- if x == y , x != z should return 2.
- if x != y != z should return 0.
- ....
you need cover situations.
Comments
Post a Comment