java如何编程实现从键盘输入10个数存入数组,显示出来并从中找出最大值?
1个回答
展开全部
import java.util.Scanner;
public class NumTest {
public static void main(String[] args) {
int[] nums=new int[10];
Scanner jin = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
nums[i]=jin.nextInt();
}
int smallerNum=nums[0];
int biggerNum=nums[0];
System.out.print("这十个数是:");
for (int i = 0; i < nums.length; i++) {
System.out.print(" "+nums[i]);
if (smallerNum>nums[i]) {
smallerNum=nums[i];
}
if (biggerNum<nums[i]) {
biggerNum=nums[i];
}
}
System.out.println();
System.out.println("最小值为:"+smallerNum);
System.out.println("最大值为:"+biggerNum);
}
}
public class NumTest {
public static void main(String[] args) {
int[] nums=new int[10];
Scanner jin = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
nums[i]=jin.nextInt();
}
int smallerNum=nums[0];
int biggerNum=nums[0];
System.out.print("这十个数是:");
for (int i = 0; i < nums.length; i++) {
System.out.print(" "+nums[i]);
if (smallerNum>nums[i]) {
smallerNum=nums[i];
}
if (biggerNum<nums[i]) {
biggerNum=nums[i];
}
}
System.out.println();
System.out.println("最小值为:"+smallerNum);
System.out.println("最大值为:"+biggerNum);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询