OJ-1038: 绝对值最大、java

题目描述

输入3个整数,输出绝对值最大的那个数。

输入

输入包含3个int范围内的整数,用空格隔开。

输出

输出三个数中绝对值最大的数,单独占一行。若绝对值最大的数不唯一,则输出最先出现的那个。例如,若输入为1 -3 3,则输出为-3;若输入为1
3 -3则输出为3。

样例输入

1 2 -3

样例输出

-3

代码示例

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main{
    
    
	public static void main(String[] args){
    
    
		Scanner sc = new Scanner(System.in);
		String numLine = sc.nextLine();
		String[] strList = numLine.split(" ");
		int result = Integer.parseInt(strList[0]);
		for (int i=1; i<strList.length; i++) {
    
    
			int num = Integer.parseInt(strList[i]);
			if (Math.abs(result) < Math.abs(num)) {
    
    
				result = num;
			}
		}
		System.out.println(result);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43796109/article/details/109383627
今日推荐