11问答网
所有问题
JAVA+查找一个数x在数组中出现的次数?
如题所述
举报该问题
推荐答案 2023-05-19
可以使用循环遍历数组,判断每个元素是否等于x,若相等则计数器加1。具体代码如下:
int[] arr = {1, 2, 3, 4, 5, 6};
int x = 3;
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
count++;
}
}
System.out.println("数" + x + "在数组中出现了" + count + "次");
温馨提示:答案为网友推荐,仅供参考
当前网址:
http://11.wendadaohang.com/zd/F7vMvvP4S8F4q4Pq7S.html
其他回答
第1个回答 2023-05-16
相似回答
一个java
编程题,怎么求出
数组中
重复
数字的出现次数
,并按次数从大
到
下...
答:
include <iostream> #include <string> #include <fstream> using namespace std; int count=0; void countNum(int a[],int start,int finish) { //cout<<start<<" \n"<<finish<<"\n"; int middle=(start+finish)/2; if(start>finish) return ; if(a[middle]==2) { count++; cou...
大家正在搜
在数组中查找某个数
c语言在数组中查找指定元素
删除数组中的某个元素
数组中查找指定元素
数组查找
查找数组元素
c语言查找数组
js删除数组中某一元素
数组