案例一: 方法练习
需求: 设计一个方法用于打印两个数中的较大数
分析: 1.定义一个方法,用于打印两个数字中的较大数,例如getMax()
2.方法中定义两个变量,用于保存两个数字
3.使用分支语句两种情况对两个数字的大小关系进行处理
4.在main()方法中调用定义好的方法
package test;
public class Test13 {
public static void main(String[] args) {
// 4.在main()方法中调用定义好的方法
getMax();
}
// 1.定义一个方法,用于打印两个数字中的较大数,例如getMax()
public static void getMax(){
// 2.方法中定义两个变量,用于保存两个数字
int number1 = 10;
int number2 = 20;
// 3.使用分支语句两种情况对两个数字的大小关系进行处理
if (number1 > number2){
System.out.println("最大的数为:"+number1);
}else{
System.out.println("最大的数为:"+number2);
}
}
}
案例二: 带参数方法练习
需求: 设计一个方法用于打印两个数中的较大数,数据来自于方法参数
分析: 1.定义一个方法,用于打印两个数字中的较大数,例如getMax()
2.为方法定义两个参数,用于接收两个数字
3.使用分支语句两种情况对两个数字的大小关系进行处理
4.在main()方法中调用定义好的方法(使用常量)
5.在main()方法中调用定义好的方法(使用变量)
package test;
public class Test14 {
public static void main(String[] args) {
// 4.在main()方法中调用定义好的方法(使用常量)
getMax(10,20);
//