求数组满足条件个数 待我称王封你为后i 2024-04-01 15:08 16阅读 0赞 **1** **问题** 给定一个数组,求出满足条件的数字个数。 ![e32e9d88c9bcbe5e4ed70b8bed78d564.png][] **2** **方法** (1) 使用main()函数,打出数组。 (2) 用循环遍历然后if判断做出统计 (3) 输出结果。 <table> <tbody> <tr> <td><strong>public class </strong>text07 { <br><strong>public static void </strong>main(String[] args) { <br><strong>int</strong>[] a={20,45,78,34,16,3,99,56};<br> 第一步:将数组打印<br><strong>int </strong>max=0;<br><strong>int </strong>min=0;<br><strong>for</strong>(<strong>int </strong>i=0;i<a.<strong>length</strong>;i++){ <br> 第二步:循环遍历,用if做判断选出符合条件的数字,并计算数目。<br><strong>if</strong>(a[i]>50){ <br> max++;<br> }<br><strong>else if </strong>(a[i]<50){ <br> min++;<br> }<br> }<br> 第三步:打印答案<br> System.<em><strong>out</strong></em>.println(<strong>"数组中大于50的有"</strong>+max+<strong>"个,小于50的有"</strong>+min+<strong>"个"</strong>);<br> }<br> }</td> </tr> </tbody> </table> **![63c9ded7ed89b5426603002a57c6d1c8.png][]** **3** **结语** 针对数组求满足条件的数的问题,提出使用循环遍历+if方法做判断,通过实验,证明该方法是有效的。还可以使用关键字new创造数组对象,再做判断。 [e32e9d88c9bcbe5e4ed70b8bed78d564.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/38d82e2300134f8fb6bae0b9f98aacab.png [63c9ded7ed89b5426603002a57c6d1c8.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/52969777f47847e6a62346eab17fbe0d.png
还没有评论,来说两句吧...