判斷一個數(shù)是偶數(shù),為何我的方法不對?
我一開始想到的方法是,假設(shè)a是一個偶數(shù),那么 a/2 取整,然后乘于2 必然完全等于 a ,可是編譯器認(rèn)為是不對的,代碼如下,請指正:
public class oushu{
? ? public static void main(String[] args){
? ? ? ? int one=20;
? ? ? ? double pan=one/2;
? ? ? ? int panduan=(int)pan;
? ? ? ? if((panduan*2)==one){
? ? ? ? ? ? System.out.println("one 是整數(shù) ");
? ? ? ? }
? ? ? ? System.out.println("one 不是整數(shù)");
? ?}
}
2016-11-07
package oushu;
public class oushu {
public static void main(String[]args){
int one=20;
double x=one/2;
if(x*2==one){
System.out.println("one是偶數(shù)");}
else {System.out.println("one不是偶數(shù)");
}
}
}
我剛開始學(xué)很多都不太明白但是這個好像可以用
2015-02-26
2015-02-24
我知道,答案是用的取余;從邏輯上思考,我的方法也是對的吧,可是為什么無法通過執(zhí)行?以為是網(wǎng)頁功能不夠完全,我下載了JDK 8u31,仍然無法執(zhí)行。。。求教這種方法的代碼!