使用Java查找最小值和最大值

使用Java查找最小值和最大值,java,Java,请帮我找出我的错误。当我按升序输入分数时,如4,5,最低分数为100。我不知道如何更改它 这是我的密码: float score=0,min=100,max=0,sum1=0,count=0,sum2=0; float average,sd; Scanner a=new Scanner(System.in); while(score>=0) { System.out.println("enter the score(a negati

请帮我找出我的错误。当我按升序输入分数时,如4,5,最低分数为100。我不知道如何更改它

这是我的密码:

   float score=0,min=100,max=0,sum1=0,count=0,sum2=0;
    float average,sd;
    Scanner a=new Scanner(System.in);

    while(score>=0)

    {
        System.out.println("enter the score(a negative score to quit)");
        score=a.nextInt();
      if(score<0)

         break;  

         if(score>=max){   
     max=score;
     sum1+=max;  



   }

      else 
     {
     min=score;
     sum2+=min;                         

     }


      count++;
             } 

      average=(sum1+sum2)/(count++ );

    System.out.println("the average : " + average);
    System.out.println( "the maximum score: "+ max);


    System.out.println("the min score: "+ min );
float score=0,min=100,max=0,sum1=0,count=0,sum2=0;
浮动平均值;
扫描仪a=新的扫描仪(System.in);
而(分数>=0)
{
System.out.println(“输入分数(要退出的负分数)”;
分数=a.nextInt();
如果(分数=最大值){
最大值=分数;
sum1+=最大值;
}
其他的
{
最小值=分数;
sum2+=min;
}
计数++;
} 
平均值=(sum1+sum2)/(计数+);
System.out.println(“平均值:“+平均值”);
System.out.println(“最大分数:+max”);
System.out.println(“最小分数:+min”);

如果(分数得到正确的最小值,则将您的
else
更改为
else

原因是,它检查
分数是否大于当前
最大值
,如果不是这种情况,那么它只是根据else假设
分数
是新的
最小值

if (score >= max) {
    max = score;
}
else if (score < min){
    min = score;
} 
// Just add the score value to the sum, it doesn´t matter if it´s min or max
sum2 += score;
count++;
if(分数>=max){
最大值=分数;
}
否则如果(分数
简化:

while((score = a.nextInt()) >= 0) {
    min = Math.min(min, score);
    max = Math.max(max, score);
    sum += score;
    average = sum / count++;
}

我认为你把问题复杂化了:你应该试着按逻辑步骤思考手头的任务:

  • 让用户输入数字
  • 将下一个整数添加到总分中
  • 检查下一个int是否>上一个已知的最大值,如果是,将上一个已知的最大值设置为下一个int的值
  • 否则,检查next int是否<上次已知的最小值,如果是,则将上次已知的最小值设置为next int的值
  • 在有更多可用输入时继续
  • 打印最高分数
  • 计算并打印平均分数(totalScore/AmountFinPuts)
  • 打印最低分数
  • 看起来是这样的:

    import java.util.*;
    
    public class MaxMinAverage {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            double score, currentMinimum = Integer.MAX_VALUE, currentMaximum = Integer.MIN_VALUE, count;
            while (in.hasNextInt()) {
                int nextInt = in.nextInt();
                if (nextInt < 0) { //break out of the loop
                    break;
                } else {
                    score += nextInt;
                    count++;
                    if (nextInt > currentMaximum) {
                        currentMaximum = nextInt;
                    } else if (nextInt < currentMinimum) {
                        currentMinimum = nextInt;
                    }
                }
            }
            System.out.println("Max: " + currentMaximum);
            System.out.println("Avg: " + (score / count));
            System.out.println("Min: " + currentMinimum);
        }
    }
    
    import java.util.*;
    公共类最大平均值{
    公共静态void main(字符串[]args){
    扫描仪输入=新扫描仪(系统输入);
    双倍分数,currentMinimum=Integer.MAX\u值,currentMaximum=Integer.MIN\u值,计数;
    while(在.hasNextInt()中){
    int-nextInt=in.nextInt();
    如果(nextInt<0){//中断循环
    打破
    }否则{
    分数+=nextInt;
    计数++;
    如果(下一个>当前最大值){
    currentMaximum=nextInt;
    }否则如果(nextInt<当前最小值){
    currentmimum=nextInt;
    }
    }
    }
    System.out.println(“最大值:+currentMaximum”);
    系统输出打印项次(“平均:”+(分数/计数));
    System.out.println(“最小值:+currentMinimum”);
    }
    }