Java数组:查找比平均值小的数字数量

huangapple 未分类评论63阅读模式
英文:

Java Array: Finding how many numbers are less then the Mean

问题

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("How many integers should we analyze?");
    int num;
    num = in.nextInt();
    while (num <= 2) {
        System.out.println("Please reenter, integer must be greater than 1");
        num = in.nextInt();
    }
    
    int[] arr = new int[num];
    System.out.println("Please enter the " + num + " integers:");
    for (int i = 0; i < arr.length; i++) {
        arr[i] = in.nextInt();
    }
    
    System.out.print("Number of integers input: " + num);
    System.out.println();
    
    double total = 0;
    for (int element : arr) {
        total += element;
    }
    System.out.print("Total: " + (int) total);
    System.out.println();
    
    double mean = 0;
    if (arr.length > 0) {
        mean = total / arr.length;
    }
    System.out.print("Mean: " + mean);
    
    int big = arr[0];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] > big) {
            big = arr[i];
        }
    }
    System.out.println();
    
    System.out.print("Largest: " + big);
    System.out.println();
    
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    int less;
    for (int i = 0; i < mean; i++) {
        int num2 = i;
        int[] arr2 = new int[num2];
        int count = 0;
        while (num2 != 0) {
            num2 /= 10;
            ++count;
            System.out.print("Numbers less than the mean: " + count);
        }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////
}

请注意,上述代码可能有一些逻辑错误,特别是在计算小于平均值的数字的部分。如果您需要更详细的帮助或修复,请随时提问。

英文:
  • Okay so im trying to find the amount of numbers that are less then
    the mean of the first array. Everything but the last part is working and i
    cant figure it out. The code at the bottom is what im having problems with.

for example. if i enter 1 2 3 4 5. the mean is 3 and, 1 and 2 are less then 3. so the answer would be 2 numbers.

public static void main(String[] args) {
		
		Scanner in=new Scanner(System.in);	
			System.out.println(&quot;How many integers should we analyze?&quot; );
				int num; 
					num=in.nextInt();				
while ( num &lt;= 2) 
{		
System.out.println( &quot;Please reenter, integer must be greater than 1&quot; );
num=in.nextInt();
}	
	 int[] arr = new int[num];
		System.out.println( &quot;Please enter the &quot;+ num +&quot; integers:&quot; );	
			for (int i = 0; i &lt; arr.length; i++) 
	{
		arr[i] = in.nextInt(); 
	}	    			
	System.out.print(&quot;Number of integers input: &quot; + num);
	System.out.println();
	
	double total = 0;
	for( int element : arr) {
		total += element;
	 
	}
	System.out.print(&quot;Total: &quot; + (int) total);
	System.out.println();
	 
	
	double mean = 0;
	 if ( arr.length &gt; 0) {
		 mean = total / arr.length;	 
	 }
	 System.out.print(&quot;Mean: &quot; + mean );
	
	 
	 int big = arr[0];
	for (int i = 0 ; i &lt; arr.length; i++) {
		if (arr[i] &gt; big) {
			big = arr[i];  			
		}
	}
	System.out.println();
	
	
	System.out.print(&quot;Largest: &quot; + big);	
	System.out.println();

///////////////////////////////////////////////////////////////////////////////////////////////    	
	
    int less;               
	for(int i=0;i&lt;mean;i++) {
		
		int num2 = i;
	int[] arr2 = new int[num2];
		int count = 0;
	while ( num2 != 0 )
	{
		num2/=10;
		++count;
		System.out.print(&quot;Numbers less than the mean: &quot; + count);
	}	
	}
//////////////////////////////////////////////////////////////////////////////////////////////

}

}

答案1

得分: 0

你可以使用下面的代码:

int count = 0;
for (int i = 0; i < arr.length; i++) {
    if (arr[i] < mean)
        count++;
}
System.out.println("小于平均值的数字个数为:" + count);

这段代码的作用是遍历所有原始整数,如果某个整数小于平均值,就将计数变量增加1。

英文:

you could use this code below

int count = 0;
for(int i =0;i&lt; arr.length;i++) {
	if(arr[i] &lt; mean)
		count++;
}
System.out.println(&quot;numbers less than mean &quot; + count);

what this does is it loops through all of the original integers and if one is less than the mean, the count variable goes up by 1.

答案2

得分: 0

你应该遍历数组并检查每个元素是否小于均值。如果是的话,就增加一个整数。

int less = 0;
for (int i = 0; i < arr.length; i++) {
    if (arr[i] < mean) {
        less++;
    }
}
System.out.print("小于均值的数字:" + less);
英文:

You should iterate over the array and check each element if they are under the mean. If yes, then increment an integer.

int less = 0;
for(int i = 0; i &lt; arr.length; i++) {
    if(arr[i] &lt; mean) {
        less++;
    }
}
System.out.print(&quot;Numbers less than the mean: &quot; + less);

huangapple
  • 本文由 发表于 2020年5月5日 06:57:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61603012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定