描述
请从标准输入流(控制台)中获取三个正整数 a,b,c,通过比较得出最大值。通过 System.out.println 语句输出最大值到标准输出流(控制台)。
1 ≤ a , b , c ≤ 10000 1leq a,b,cleq 10000 1≤a,b,c≤10000
样例
评测机会将整个项目的代码编译为一个可执行的 Main 程序,并按照这样的方式执行你的代码 Main,你的代码需要从标准输入流(控制台)中读入数据 a,b,c,计算出结果后并打印到标准输出流(控制台)中。
样例一
当 a = 3, b = 7, c = 2 时,程序执行打印出的结果为:
7
【解法一】:if语句、赋值
【解题思路】:
首先假设第一个数为最大值,将第一个数赋值给最大值变量,接下来用第二个数与当前最大值变量进行比较,若第二个数大于最大值变量,则将第二个数赋值给当前最大值变量,第三个数与第二个数操作相同,最后输出最大值变量即可。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
// read data from console
// output the answer to the console according to the
// requirements of the question
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int max_num = a;
if(b>max_num)
{
max_num = b;
}
if(c>max_num)
{
max_num = c;
}
System.out.println(max_num);
}
}
【解法二】:max()方法
【解题思路】:
使用 max()方法先求得前两个数中的最大值,再用 max()方法让前两个数中的最大值与第三个数作为参数求最大值
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
// 使用 max()方法先求得前两个数中的最大值,再用 max()方法让前两个数中的最大值与第三个数作为参数求最大值
System.out.println(Math.max(Math.max(a, b), c));
}
}



