如果输入一个整数 可以被2 除尽 就返回2,如果被3除尽就返回3,如果5除尽返回5. public  int divisiable(int  N)
{
  if(N % 2 == 0)
  { 
    return 2;
  }
   else if(N % 3 == 0)
  { 
    return 3;
  }
  else if(N % 5 == 0)
  { 
    return 5;
  } else
{
  return -1; // 都不满足返回-1
}
}大家看看我写的这个小函数有什么问题,如果有,怎么改呢?谢谢了。

解决方案 »

  1.   

    [Java code]
     public  int divisiable(int  N)
    {
      if(N==0){
        return -1;
      }else if(N % 2 == 0)
      { 
        return 2;
      }
       else if(N % 3 == 0)
      { 
        return 3;
      }
      else if(N % 5 == 0)
      { 
        return 5;
      } else
    {
      return -1; // 都不满足返回-1
    }
    }
    [/Java code]
      

  2.   

    public  int[] divisiable(int  N)??
      

  3.   

    这个程序如果是 6 输入进去就返回 2 了
    如果按楼主的意思的话
    前边应该加个
    if( n % 6 == 0 || n % 15 == 0 || n % 10 == 0)
    return -1 ; 
      

  4.   

    import java.util.Scanner;public class HelloWorld { static Scanner sc = new Scanner(System.in);
    static int i = sc.nextInt(); public static void main(String[] args) {
    int y = divisiable(i);
    System.out.println(y); } public static int divisiable(int x) {
    if (x != 0) {
    if ((x % 2 == 0) && (x % 3 != 0) && (x % 5 != 0)) {
    return 2;
    } else if ((x % 3 == 0) && (x % 5 != 0)) {
    return 3;
    } else if (x % 5 == 0) {
    return 5;
    }
    } else {
    return -1;
    }
    return 0;
    }
    }
      

  5.   

    其实 楼上的方法都很不专业
    google里搜 欧几里得公式 你就什么都明白了!