public class fiboaddeven {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=-1;
int b=1;
int c=0;
for(int i=1;i<=12;i++){
c=a+b;
a=b;
b=c;
System.out.println(c);
}
}
}
The Following output is 0 1 1 2 3 5 8 13 21 34 55 89 I want 0odd 1odd 1odd 2even 3odd 5odd 8even 13odd 21even 34odd 55odd 89odd
What you want to use is % operator with returns remainder.
String evenOrOdd = c % 2 == 0 ? " even" : " odd";
System.out.println(c + evenOrOdd);
除以2时,只需测试其余部分即可:
System.out.println(c + (( c % 2 == 0 ) ? "even" : "odd" ));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.