zoukankan      html  css  js  c++  java
  • 习题4 编写一个方法method(),判断一个数能否同时被3和5整除

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

    首先编写一个方法method(),由于是判断,所以返回的数据类型应是波尔型,并且向主方法传一个整数类型的参数X

    public class Zhawo11 {
        
        }
        
        public static boolean method(int x){
            
        }
    }

    接下来在子方法里判断,如果能同同时被3和5整除,就返回true,否则返回false

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

    在主方法里定义变量b接受这个返回值

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

    输出,此处用到三目运算符(b==true? "能":“不能”) 当b为true时 输出“能”,当b为false时,输出“不能”

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

    接下来向主方法传入任意参数

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

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

    结果

  • 相关阅读:
    PHP保留小数的相关方法
    ASP.NET Core MVC 之过滤器(Filter)
    ASP.NET Core MVC 之控制器(Controller)
    ASP.NET Core MVC 之视图组件(View Component)
    ASP.NET Core MVC 之局部视图(Partial Views)
    标签助手(TagHelper)
    ASP.NET Core MVC 之布局(Layout)
    ASP.NET Core MVC 之视图(Views)
    ASP.NET Core MVC 之模型(Model)
    九卷读书:淘宝从小到大的发展 -重读《淘宝技术这十年》
  • 原文地址:https://www.cnblogs.com/FrankLiner/p/7517382.html
Copyright © 2011-2022 走看看