练习1
在java中三元运算符是基础部分,懂得它的运用,即可。
1,使用三元运算符,获取两个数中较大的数
2,使用三元运算符,获取三个数中较大的数
2,使用三元运算符,获取三个数中较大的数
import java.util.Scanner;
import org.junit.Test;
public class test1 {
@Test
public void test(){
//使用三元运算符,获取两个数中较大的数
//使用三元运算符,获取三个数中较大的数
int[] number = new int[3];
for(int i=0; i<number.length; i++){
Scanner x1 = new Scanner(System.in);
number[i] = x1.nextInt();
}
int a = number[0] > number[1] ? number[0]:number[1];
int c = a > number[2] ? a:number[2];
System.out.println("两个数中的最大数为:"+a);
System.out.println("三个数中的最大数为:"+c);
}
}
这道题我使用了数组,不过还有更简单的做法,如果你想在三元运算符中使用数组,这道题是可以用做参考的哟。