考试题1

本文介绍了一个简单的Java方法,用于判断一个整数是否可以同时被3和5整除。通过在主方法中调用该方法并传入一个整数参数,程序将输出该数字是否能够被3和5整除。

编写一个方法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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值