[英]print each number from the array and the times it has been repeated
I have an array containing the below nos: 1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1 I need to print each number from the array and the times it has been repeated in the array. 我有一个包含以下编号的数组:1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43 ,2,4,7,7,5,2,1,3,4,6,311,1我需要打印数组中的每个数字以及它在数组中重复的次数。 I am receiving an error while running the below program "i cannot be resolved to a variable". 运行以下程序“我无法解析为变量”时出现错误。
public class PrintNosandRepeatation {
public static void main(String[] args) {
int a[] = new int[] {1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1};
for (int i=0; i< a.length; i++);
System.out.println(a[i]);
}
}
Please let me know where i am going wrong.. 请让我知道我要去哪里了..
Delete the ;
删除;
after the if
condition: 在if
条件之后:
for (int i=0; i< a.length; i++);
^------------- delete this
Why? 为什么? Because it declares an empty statement, so the for
body will be empty. 因为它声明了一个空语句,所以for
主体将为空。 Therefore when you try to use variable i
, it is actually out of the block it was declared in (a block is delimited by { }
). 因此,当您尝试使用变量i
,它实际上不在声明它的块之内(一个块用{ }
分隔)。
int a[]= {1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1}; int a [] = {1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2, 4,7,7,5,2,1,3,4,6,311,1};
int i, j;
System.out.println("Repeated Elements are :");
for (i = 0; i < a.length; i++) {
for (j = i + 1; j < a.length; j++) {
if (a[i] == a[j])
System.out.print(a[i] + " ");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.