day07-匿名对象的介绍
This commit is contained in:
@@ -1,15 +1,34 @@
|
|||||||
package com.inmind.scanner01;
|
package com.inmind.scanner01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
常用类-Scanner-练习2_录入n个整数求最大值
|
常用类-Scanner-练习2_录入n个整数求最大值
|
||||||
*/
|
*/
|
||||||
public class Demo03 {
|
public class Demo03 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//n可以手动赋值,也可以用户赋值
|
//n可以手动赋值,也可以用户赋值
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("请输入要计算的整数个数:");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
|
||||||
//获取n个整数值(for循环),保存到一个容器(数组)
|
//获取n个整数值(for循环),保存到一个容器(数组)
|
||||||
|
int[] arr = new int[n];
|
||||||
|
//n次循环
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
System.out.println("请输入第"+i+"个整数");
|
||||||
|
arr[i-1] = sc.nextInt();
|
||||||
|
}
|
||||||
//对容器中的值,取最大值
|
//对容器中的值,取最大值
|
||||||
|
int max = arr[0];
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int temp = arr[i];
|
||||||
|
if (temp > max) {
|
||||||
|
max = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//打印结果
|
//打印结果
|
||||||
|
System.out.println("您输入的"+n+"个整数的最大值为:"+max);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
day07/src/com/inmind/scanner01/Demo04.java
Normal file
22
day07/src/com/inmind/scanner01/Demo04.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.scanner01;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
匿名对象:没有名字的对象
|
||||||
|
匿名对象的使用场景:当一个对象只要被使用一次的时候,可以使用匿名对象来简单实现
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//只取一个整数时
|
||||||
|
/*Scanner sc = new Scanner(System.in);
|
||||||
|
int result = sc.nextInt();
|
||||||
|
System.out.println(result);*/
|
||||||
|
|
||||||
|
/*int result = new Scanner(System.in).nextInt();
|
||||||
|
System.out.println(result);*/
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(new Scanner(System.in).nextInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user