考试题1

编写一个方法method(),判断一个数能否同时被3和5整除

编写方法method

public class test{
   public static void main(String[] args){ 
    
  }
  public static boolean method(){
  }
}

在子方法里判断是否能被3和5整除

public class test {
    public static void main(String args[]){
    
    }
    
    public static boolean method(int x){
        if(x%3==0 && x%5==0){
            return true;
        }else{
            return false;
        }
        
    }
}

在主方法里定义一个变量接收这个返回值

public class test {
    public static void main(String args[]){
           boolean y=method;
    }
    
    public static boolean method(int x){
        if(x%3==0 && x%5==0){
            return true;
        }else{
            return false;
        }
        
    }
}

输出 在主方法里传参数

public class test {
    public static void main(String args[]){
        boolean y=method(15);
        System.out.println("这个数字"+(y==true? "能":"不能")+"被整除");
    }
    
    public static boolean method(int x){
        if(x%3==0 && x%5==0){
            return true;
        }else{
            return false;
        }
        
    }
}

运行结果:

  

原文地址:https://www.cnblogs.com/zyn0216/p/7501888.html