求出3个变量中的最大值和最小值?用java程序编写并且要用上if-else。

2024-11-22 17:41:49
推荐回答(3个)
回答1:

package test.yzy;

public class TestMaxMix {

/**
* @param args
*/
public static void main(String[] args)
{
int a=22, b=111, c=9;
test(a,b,c);

}

private static void test(int a, int b, int c)
{
int maxB;
int mixB;
if (a>b)
{
maxB = a;
mixB = b;
}
else
{
maxB = b;
mixB = a;
}

if (maxB > c)
{

}
else
{
maxB = c;
}

if (mixB < c)
{

}
else
{
mixB = c;
}

System.out.println("最大值:" + maxB + "最小值:" + mixB);
}
}

回答2:

写个不用if-else的吧
max=(a>b?a:b)>c?(a>b?a:b):c
min=(a>b?b:a)>c?c:(a>b?b:a)

回答3:

一楼正解!