判断方法(奇数):
错误判断方法:通过a%2==1来判断。(原因:负奇数对2取余的结果为-1)
正确判断方法:(1) 通过a%2!=0来判断。
(2) 通过(a&1)==1来判断。(推荐使用,位操作,性能更优)
https://www.cnblogs.com/yongh/p/9276904.html
https://www.cnblogs.com/pigga/p/10098288.html
package test;
public class Test26 {
public static void main(String[] args) {
int count = 0;
int sum = 0;
int n = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
System.out.print(i + " ");
count++;
n++;
sum += i;
if (n == 9) {
System.out.print("\n---------------------------------\n");
n = 0;
}
}
}
System.out.print("\n---------------------------------\n");
System.out.println("count = " + count);
System.out.println("sum = " + sum);
}
}