是否有可能找到高于和低于平均值的方法

时间:2019-04-04 13:20:46

标签: java

我正在创建一个程序,以查找用户输入的所有数字的平均值,并存储这些数字以检查输入的数字是低于还是高于所计算的平均值。 我的程序输出的所有输入数字均低于平均值。我已经检查了堆栈溢出是否存在类似的问题,我已经尝试了所有这些,但是我的输出仍然仅低于平均值显示

这就是我尝试过的

public void newspaper()
    {
        System.out.println("Question 4 \n");
        int youth;
        double avg =0;
        int sum = 0;
        int numYouth = 5;

   //The loop for calculating the average
    for (int i = 1; i <= 5; i++) 
    {
        System.out.println("Youth " + i + " How many was delivered?");
        youth = in.nextInt();

         sum = sum + youth;
         avg = sum / numYouth;


    }
       System.out.println("Average is: " + avg+ "\n");

        double aboveAvg = 0;

      //The loop for checking below of above average
        for (int j = 1; j <=5; j++) 
       {
           if(aboveAvg > avg)
           {                
               System.out.println("Youth " + j + " is above average");
               aboveAvg++;
           }
           else 
          {
              System.out.println("Youth " + j + " below average");

          }
       }

  }

4 个答案:

答案 0 :(得分:1)

这是可能解决您问题的方法: 请注意,您需要存储用户输入,一次计算平均值(不在for循环内),最后将存储的数字与之前计算的平均值进行比较。

admin

答案 1 :(得分:0)

假设您要尝试“找到用户输入的所有数字的平均值”,请存储这些数字以检查输入的 每个数字 是否低于或高于计算的平均值”,则需要解决以下问题:

  1. “存储这些数字”部分
  2. 将计算出的平均值与存储的数字进行比较。

可能的解决方案:

  • 使用列表或数组存储用户输入的数字。
  • 只要开始读取数字之前就知道要存储的元素数,就可以使用数组。
  • 如果要将输入的值与计算的平均值进行比较,请从列表/数组中读取值。

    public void newspaper()
    {
        System.out.println("Question 4 \n");
        int youth;
        double avg =0;
        int sum = 0;
        int numYouth = 5;
    
        // Create a list to store the entered values
        // List<Integer> enteredNumbers = new ArrayList<Integer>(); 
    
        // Using an array of '5' elements - this 5 comes from numYouth
        int[] enteredNumbers = new int[numYouth]; // better not to 'hardcode'
    
        //The loop for calculating the average
        for (int i = 1; i <= numYouth; i++) 
        {
           System.out.println("Youth " + i + " How many was delivered?");
           youth = in.nextInt();
    
           enteredNumbers[i-1] = youth; // array is 0-indexed
           sum = sum + youth;
           avg = sum / numYouth;    
       }
    
       System.out.println("Average is: " + avg+ "\n");
    
       // an int is enough to track the number of values above the average
       int aboveAvg = 0; 
    
       //The loop for checking below of above average
       for (int j = 1; j <= numYouth; j++) 
       {
           // compare stored value against the average calculated above
           if(enteredNumbers[j-1] > avg) // array is 0-indexed
           {                
               System.out.println("Youth " + j + " is above average");
               aboveAvg++;
           }
           else 
           {
              System.out.println("Youth " + j + " below average");
           }
       }
       System.out.println(aboveAvg + " Youths are above average");
    

    }

答案 2 :(得分:0)

您需要将数字存储在临时列表中,并使用计数器“ ctr”来增加匹配大小写的值。为了简单起见,我为每个循环都使用了

Sub CreateCommands()
    Dim oCommands As New List(Of MyCommand)

    oCommands.Add(New MyCommand("DrawLogoCmd", "Draw Logo", NameOf(DrawLogo)))
    oCommands.Add(New MyCommand("PublishDrawingCmd", "Publish Drawing", NameOf(PublishDrawing)))
    ' [Dozens more commands]

    For Each oCommand As MyCommand In oCommands
        ' Code for adding internal command definition and button to Inventor. This is working fine.
        Dim oCommandDef As Inventor.CommandDefinition = InventorApp.CommandDefinitions.Add(oCommand.InternalDefinitionName, oCommand.DisplayName)
        InventorApp.ToolbarButtons.Add(oCommandDef)

        ' Associate command definition with handler function
        AddHandler oCommandDef.OnExecute, Sub(sender As Object, e As EventArgs) CallMethod(oCommand.Handler)
    Next
End Sub

Private Sub CallMethod(name As String)
    Me.GetType().GetMethod(name).Invoke(Me, Nothing)
End Sub

Sub DrawLogo()
    ' [My add-in's code to draw logo in Inventor]
End Sub

Sub PublishDrawing()
    ' [My add-in's code to publish drawing in Inventor]
End Sub

答案 3 :(得分:0)

尝试使用数组而不是变量 参见下面的代码

nslookup