day07-常用类-scanner的练习
This commit is contained in:
@@ -1,5 +1,18 @@
|
|||||||
package com.inmind.scanner01;
|
package com.inmind.scanner01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
//键盘录入两个整数求和
|
//键盘录入两个整数求和
|
||||||
public class Demo02 {
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
//接收2个整数
|
||||||
|
System.out.println("请输入一个整数:");
|
||||||
|
int a = sc.nextInt();
|
||||||
|
System.out.println("请输入第二个整数:");
|
||||||
|
int b = sc.nextInt();
|
||||||
|
//求和
|
||||||
|
System.out.println("和值:"+(a+b));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,30 @@
|
|||||||
package com.inmind.scanner01;
|
package com.inmind.scanner01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
//键盘录入三个整数求最大值(可以动态地创建数组,来接收输入的内容,使用for来简化重新的代码)
|
//键盘录入三个整数求最大值(可以动态地创建数组,来接收输入的内容,使用for来简化重新的代码)
|
||||||
public class Demo03 {
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建Scanner对象
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("请您输入要比较大小的整数的个数:");
|
||||||
|
|
||||||
|
int num = sc.nextInt();//由用户决定
|
||||||
|
//动态初始化创建长度为3的数组
|
||||||
|
int[] arr = new int[num];
|
||||||
|
//for循环遍历,获取用户输入的整数
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
System.out.println("请输入第"+(i+1)+"个整数:");
|
||||||
|
arr[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
//计算数组中哪个值最大
|
||||||
|
int max = arr[0];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
if (arr[i] > max) {
|
||||||
|
max = arr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("您输入的最大值为:"+max);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user