gpt4 book ai didi

java - 在Java中显示最小的数字

转载 作者:行者123 更新时间:2023-12-01 23:25:58 25 4
gpt4 key购买 nike

我花了几天时间试图找出解决这个问题的方法,但找不到解决方案。我已经尝试了所有的建议,但我仍然不知道如何显示最小的数字。

 import java.util.Scanner;


public class finalname {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);

System.out.println("How Many Numbers You Want To Enter: ");
int total = kb.nextInt();
int input = 0;
int sum = 0;
int average=0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
while (input < total) {
input++;

System.out.println("Enter " + input + ". Operand: ");

sum += kb.nextInt();

if (min >= sum)
{
min = sum;
}
if (max <= sum)
{
max = sum;
}



average = ( sum ) / ( input);
}
System.out.println("The sum is " + sum + ".");
System.out.println("The avg " + average);
System.out.println("The highest number " + max);
System.out.println("The the lowest number " + min);


}
}

最佳答案

为什么你+= sum?如果您只是想从输入数字中获取最小数字。比改变从

 sum += kb.nextInt();

  int n=kb.nextInt();
sum += n;// += will sum your input. It is only need for average.

if(min>n)// To get smallest number from input

关于java - 在Java中显示最小的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com