题目:
键盘输入三个int数字,然后求出其中的最大值。
思路:
1.既然是键盘输入,肯定需要用到Scanner
2.Scanner三个步骤:导包、创建、使用nextInt()方法
3.既然是三个数字,那么调用三次nextInt()方法,得到三个int变量
4.无法同时判断三个数字谁最大,,应该转换为两个步骤,
(1)首先判断前两个数当中谁最大,拿到二者的最大值
(2)拿到二者最大值后和第三个数字进行比较,得到三个数字当中
的最大值。
5.打印最终结果。
代码如下:
package 类和对象;
import java.util.Scanner; // 1.导包
public class Demo05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数字:");
int a = sc.nextInt();
System.out.println("请输入第二个数字:");
int b = sc.nextInt();
System.out.println("请输入第三个数字:");
int c = sc.nextInt();
//首先比较前两个数字取最大值
int temp = a > b ? a : b;
int max = temp > c ? temp : c;
System.out.println("您输入的三个数字中最大的数为:"+max);
}
}