java中选出最大值最小值
时间: 2024-06-15 15:05:41 浏览: 16
在Java中,可以使用以下方法来选出最大值和最小值:
1. 使用循环遍历数组或集合,比较每个元素与当前最大值和最小值,更新最大值和最小值。
例如,对于整型数组 `int[] nums`:
```java
int max = Integer.MIN_VALUE; // 初始化最大值为最小整数
int min = Integer.MAX_VALUE; // 初始化最小值为最大整数
for (int num : nums) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
```
2. 使用Java 8的流(Stream) API,可以更简洁地选出最大值和最小值。
例如,对于整型数组 `int[] nums`:
```java
int max = Arrays.stream(nums).max().getAsInt();
int min = Arrays.stream(nums).min().getAsInt();
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
```
相关问题
点击复制后,将打开C知道体验页 用Java写一个输入三个整数,选出最大值和最小值,输出最大值和最小值的程序
以下是用Java写一个输入三个整数,选出最大值和最小值,输出最大值和最小值的程序:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int max = a;
int min = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
System.out.println("最大值是:" + max);
System.out.println("最小值是:" + min);
}
}
```
c语言程序设计 随机生成十道题包括加减乘除判断正误并求出平均值选出最大值最小值,将正确结果从大到小和从小到大排序
好的,我来给你写一个随机生成十道加减乘除题目的c语言程序,然后通过用户输入答案进行判断正误,并计算平均值,选出最大值和最小值,并将正确结果从大到小和从小到大排序。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
int a, b, c, result, answer;
int correct_count = 0;
int sum = 0;
int max = 0, min = 100;
int results[10];
printf("随机生成十道加减乘除题目,你需要输入答案并判断正误\n");
srand((unsigned)time(NULL)); // 设置种子
for (i = 0; i < 10; i++)
{
a = rand() % 100;
b = rand() % 100;
c = rand() % 4; // 随机生成加减乘除符号
switch (c)
{
case 0:
result = a + b;
printf("%d + %d = ", a, b);
break;
case 1:
result = a - b;
printf("%d - %d = ", a, b);
break;
case 2:
result = a * b;
printf("%d * %d = ", a, b);
break;
case 3:
result = a / b;
printf("%d / %d = ", a, b);
break;
}
scanf("%d", &answer); // 用户输入答案
if (answer == result)
{
printf("正确!\n");
correct_count++;
results[i] = result;
}
else
{
printf("错误!\n");
results[i] = -1;
}
sum += result;
if (result > max)
max = result;
if (result < min)
min = result;
}
printf("你一共回答了 %d 道题,其中 %d 道题回答正确\n", 10, correct_count);
printf("这十道题的平均值是 %d\n", sum / 10);
printf("其中最大值是 %d,最小值是 %d\n", max, min);
// 从大到小排序
for (i = 0; i < 10; i++)
{
int j, temp;
for (j = i + 1; j < 10; j++)
{
if (results[i] < results[j])
{
temp = results[i];
results[i] = results[j];
results[j] = temp;
}
}
}
printf("从大到小排序的结果为:\n");
for (i = 0; i < 10; i++)
{
if (results[i] != -1)
printf("%d ", results[i]);
}
printf("\n");
// 从小到大排序
for (i = 0; i < 10; i++)
{
int j, temp;
for (j = i + 1; j < 10; j++)
{
if (results[i] > results[j])
{
temp = results[i];
results[i] = results[j];
results[j] = temp;
}
}
}
printf("从小到大排序的结果为:\n");
for (i = 0; i < 10; i++)
{
if (results[i] != -1)
printf("%d ", results[i]);
}
printf("\n");
return 0;
}
```
希望这个程序能够帮到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)