Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数组存储为:
static int[] number = new int[50];
我想编写一段代码来计算某些整数在数组中出现的次数。说数字 1。
在这种情况下,我建议您尝试这样做。从一个简单的例子开始,添加一个循环来遍历数组并计算出现次数。最后打印答案。
下一步是使“countOccurances”成为一个方法,该方法采用数组、搜索的值并返回出现次数。
顺便说一句:除非您无法避免,否则我不会将数组设为静态。
如果你想要一个具体的答案,我建议就你编写的代码提出一个具体的问题,包括你期望发生的事情和实际发生的事情。
在循环上迭代您的数组并检查是否增加您的计数器。