java代码库01——定义成员方法

拒绝窘境,当你不停滞不前的时候,就已经在退步了。

Akino·杜菲     版本声明:原创内容,请勿转载,否则将追究法律责任!

1.定义方法,求三个int数的最大值:

  用两种方法:①if else;②三元运算符

package com.test;
/*
 * 定义方法,用于获取三个int数中最大的
 */

public class Practise {
    
    public static int getMax1(int a,int b,int c){
        int max;
        if(a>b){
            if(a>c){
                max = a;
            }else{
                max = c;
            }
        }else{
            if(b>c){
                max = b;
            }else{
                max = c;
            }
        }
        return max;
    }
    
    public static int getMax2(int a,int b,int c ) {

        return (a>b)?((a>c)?a:c):((b>c)?b:c);
    }

    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 30;
        System.out.println("最大值为:"+getMax1(a, b, c));
        System.out.println("最大值为:"+getMax2(a, b, c));
    }
View Code

2.定义方法,接收一个int值,返回该值对应的是星期几,要求使用switch结构实现

  用两种方法,一种很多return,另一种设一个变量,用break,只有一个return,推荐用第二种。

/*
 * 定义方法,接收一个int值,返回该值对应的是星期几,要求使用switch结构实现
 * 
 */
package com.test;

import java.sql.ResultSet;
import java.util.Scanner;

public class Practise2 {

    public static String getWeek1(int week){
        switch (week) {
        case 1:
            return "星期一";
        case 2:
            return "星期二";
        case 3:
            return "星期三";
        case 4:
            return "星期四";
        case 5:
            return "星期五";
        case 6:
            return "星期六";
        case 7:
            return "星期日";
        default:
            return "输入非法";
        }
        
    }
    /*
     * 通常,定义一个变量,用于保存最终要的返回的值
     * 在不同的case中对这个变量进行进行赋值
     * 然后返回这个变量
     */
    public static String getWeek2(int week) {
        String res = "";
        switch (week) {
        case 1:
            res = "星期一";
            break;
        case 2:
            res = "星期二";
            break;
        case 3:
            res = "星期三";
            break;
        case 4:
            res = "星期四";
            break;
        case 5:
            res = "星期五";
            break;
        case 6:
            res = "星期六";
            break;
        case 7:
            res = "星期日";
            break;
        default:
            res = "非法字符";
            break;
        }
        return res;
    }
    
    
    
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请您输入一个1~7之间的数字:");
        int week = scanner.nextInt();
        String reString = getWeek1(week);
        System.out.println("您输入的是:"+ week +" ,对应的日期是:"+getWeek1(week));
        System.out.println("您输入的是:"+ week +" ,对应的日期是:"+getWeek2(week));
        
    }
    

}
View Code

3.定义方法,用于显示两个int值的四则运算

  返回值类型是void记住,当然你也可以返回值写个其他的,不用就可以

package com.test;

import java.util.Scanner;

/*
 * 1.定义一个方法,用于显示两个int值的四则运算
 */
public class Practise3 {
    public static void show(int a,int b){
        System.out.println(a+"+"+b+"="+(a+b));
        System.out.println(a+"-"+b+"="+(a-b));
        System.out.println(a+"*"+b+"="+(a*b));
        System.out.println(a+"/"+b+"="+(a/b));
        
    }
    
    
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入第一个数:");
        int a = scanner.nextInt();
        
        System.out.print("请输入第二个数:");
        int b = scanner.nextInt();
        
        show(a, b);

    }

}
View Code

4.定义方法,用于判断一个int数是否为偶数

  在写方法时,需要2个确定,即返回值类型,和形参列表

package com.test;

import javax.print.attribute.standard.RequestingUserName;

/*
 * 定义方法啊,用于判断一个int数是否为偶数
 * 用于判断的方法,返回值都是布尔类型
 * true和false具体代表什么含义,需要自己定义
 */
public class Practise4 {
    public static boolean isEven(int i){
        if(i%2 ==0){
            return true;
        }else {
            return false;
        }
    }
        
    public static void main(String[] args) {
        int a = 30;
        int b = 11;
        System.out.println(a+"是否为偶数?"+isEven(a));
        System.out.println(b+"是否为偶数?"+isEven(b));

    }

}
View Code

5.定义方法,用于打印任意行数和列书的“+”号

  注意,在第一层循环下加个换行,英文中行是row,列是column。

/*
 * 定义方法,用于打印任意行数和列书的“+”号
 */
package com.test;

import java.util.Scanner;

public class Practise5 {
    
    public static void print1(int row, int column){
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print("+");    
            }
            System.out.println();
        }
    }
    
    
    
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        System.out.print("请输入行数:");
        int row = a.nextInt();
        
        System.out.print("请输入列数:");
        int column = a.nextInt();
        
        print1(row,column);
    }

}
View Code

6.定义方法,用于打印99乘法表,任意输入一个int值,即可打印该值的乘法表

  注意:/t是制表符,可以让99乘法表格式一致,返回值类型是void,参数列表是int a

/*
 * 定义方法,用于打印99乘法表
 * 从键盘录入一个数,用于表示乘法表的行数
 *     例如:输入8,则打印8*8乘法表
 */
package com.test;

import java.util.Scanner;

public class Practise6 {
    public static void mult(int a){
        for (int i = 1; i <= a; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+i*j+"	");
            }
            System.out.println();
            
        }
    }
    
    
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请您输入一个数:");
        int a = scanner.nextInt();
        mult(a);

    }

}
View Code

7.定义方法,判断两个值的大小

  注意:这个代码可以改成三元运算符方式即:    a>b?a:b

/*
 * 定义方法,用于获取两个int数中的最大值
 * 思路:
 *         if结构
 *         三元运算符
 */
package com.test;

public class Practise7 {
    public static int getmax(int a,int b) {
        if(a>b){
            return a;
        }else{
            return b;
        }    
    }
    
    public static void main(String[] args) {
        int a = 10;
        int b = 17;
        
        System.out.println("最大值是:"+getmax(a, b));

    }

}
View Code

你不愿意改变的时候,往往就是你最需要改变的时候。让你变得更加专业,成为一个it精英。只有经历才能让我们更加认识自己,不经历历练就无法看见阳光。 欢迎加入BigBang高级大数据之路,qq群号:945694891

原文地址:https://www.cnblogs.com/akinodoo/p/10064816.html