day02-方法的定义和调用的练习

This commit is contained in:
2026-07-16 13:36:54 +08:00
parent 8f15a68ee9
commit 3eb476108e

View File

@@ -12,7 +12,38 @@ package com.inmind.yunsuan02;
如果表达式1的结果为false那就执行表达式3将结果返回赋值给左侧变量 如果表达式1的结果为false那就执行表达式3将结果返回赋值给左侧变量
*/ */
public class Demo12 { public class Demo12 {
public static void main(String[] args) { public static void main(String[] args) {
//调用获取最大值的方法
getMax();
//调用获取最小值方法
getMin();
//调用获取变量名最大值方法
getNameMax();
}
public static void getNameMax(){
//定义2个整数变量
int a = 10;
int b = 20;
//判断哪个变量大??(表示出哪个变量名) 变量a大或者变量b大
String result = a>b?"变量a大值为"+a:"变量b大值为"+b;
System.out.println(result);
}
public static void getMin(){
//定义2个整数变量
int a = 10;
int b = 20;
//判断下哪个整型变量的值小???
int min = b<a?b:a;
System.out.println(min);
}
//获取两个变量中的最大值
public static void getMax() {
//定义2个整数变量 //定义2个整数变量
int a = 10; int a = 10;
int b = 20; int b = 20;
@@ -20,13 +51,5 @@ public class Demo12 {
//判断哪个变量的值大?? //判断哪个变量的值大??
int max = a>b?a:b; int max = a>b?a:b;
System.out.println(max); System.out.println(max);
//判断下哪个整型变量的值小???
int min = b<a?b:a;
System.out.println(min);
//判断哪个变量大??(表示出哪个变量名) 变量a大或者变量b大
String result = a>b?"变量a大值为"+a:"变量b大值为"+b;
System.out.println(result);
} }
} }