package java1to10;

public class D3_IfThenElse {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int tempInt1 = 5, tempInt2 = 0;
		// 判断tempInt1,给tempInt2赋值
		if (tempInt1 >= 0) {
			tempInt2 = tempInt1;
		} else {
			tempInt2 = -tempInt1;
		}
		System.out.println(String.format("1,绝对值是:%d", tempInt2));

		tempInt1 = -10;
		if (tempInt1 >= 0) {
			tempInt2 = tempInt1;
		} else {
			tempInt2 = -tempInt1;
		}
		System.out.println(String.format("2,绝对值是:%d", tempInt2));

		// 现在调用方法来实现绝对值的输出
		System.out.print(String.format("3,绝对值是:%d", getAbs(-13)));
	}

	public static int getAbs(int absInt) {
		if (absInt >= 0) {
			return absInt;
		} else {
			return -absInt;
		}
	}
}

控制台输出结果

1,绝对值是:5
2,绝对值是:10
3,绝对值是:13