[英]How do I take the repeated values in an array and print out the number of duplicates using a for loop ?(Java)
我编写了以下程序,它在 doubleArray 中获取重复项并将它们添加到计数器中。
然后我写了一个 for 循环来打印出数组中的值,从最小值开始,增加 0.1,到最大值。 然后程序应该在数组中表示的每个数字后放置一个美元符号。
double[] doubleArray = {1.7,1.7,2.0,1.3,1.0};
int count = 0;
Arrays.sort(doubleArray);
for(int i = 0;i<doubleArray.length-1;i++){
for(int j =i+1;j<doubleArray.length;j++){
if (doubleArray[i] == doubleArray[j]) {
count++;
} else {
break;
}
}
}
int finalDub = doubleArray.length;
double min = doubleArray[0];
double max = doubleArray[finalDub - 1];
for (double i = min; i < max+0.1; i += 0.1) {
System.out.printf("%.1f ", i);
System.out.print("$".repeat(count));
System.out.print("\n");
}
但是当我运行代码时,会输出以下内容
1.0 $
1.1 $
1.2 $
1.3 $
1.4 $
1.5 $
1.6 $
1.7 $
1.8 $
1.9 $
2.0 $
当它应该是以下内容时,因为我希望它仅在数组中表示的双精度数之后添加 $,并为重复值添加多个“$”。
1.0 $
1.1
1.2
1.3 $
1.4
1.5
1.6
1.7 $$
1.8
1.9
2.0 $
我相信正在发生的事情是计数 integer 被设置一次并且永远不会更新。 无论哪种方式,我如何更新我的计数器逻辑来表示我想要输出的内容?
我制作了一个自定义计数器 class 来跟踪每个 Double 和 Double 的计数。 您可以遍历所有双打,添加非重复项,如果重复,则增加计数。
import java.util.ArrayList;
import java.util.Arrays;
public class test {
static class Counter{
Double value;
int count;
Counter(Double d, int c){
this.value = d;
this.count = c;
}
public String toString() { return value+":"+count; }
}
public static void main(String[] args){
double[] doubleArray = {1.7,1.7,2.0,1.3,1.0};
ArrayList<Counter> list = new ArrayList<>();
Arrays.sort(doubleArray);
for(int i = 0;i<doubleArray.length;i++){
int size = list.size();
if (size == 0){
list.add(new Counter(doubleArray[0], 1));
}else{
Counter current = list.get(size-1);
if (doubleArray[i] != current.value){
list.add(new Counter(doubleArray[i], 1));
}else{
current.count = current.count +1;
}
}
}
// for (int i=0; i< list.size(); i++){
// System.out.println(list.get(i).toString());
// }
int finalDub = doubleArray.length;
double min = doubleArray[0];
double max = doubleArray[finalDub - 1];
for (double i = min; i < max+0.1; i += 0.1) {
int count = 0;
System.out.printf("%.1f ", i);
for (int j=0; j< list.size(); j++){
Counter obj = list.get(j);
double val = Double.parseDouble(String.format("%.1f", i));
//this is necessary to format the floating point number to the exact accuracy stored in your array
if (obj.value == val){
count = obj.count;
break;
}else{
continue;
}
}
System.out.print("$".repeat(count));
System.out.print("\n");
}
}
}
BUG FOUND o_o(仅供参考)
请注意,这不是一个完整的解决方案,只是一个建议
与其使用 break 语句创建嵌套循环,我 1000% 推荐一种更接近于此的方法。
int size = doubleArray.length - 1, count = 0;
double number = doubleArray[0];
for(int index = 1; index < size; index++)
{
if(number == doubleArray[index])
{
count++;
}
number = doubleArray[index + 1];
}
虽然我不知道你的代码到底出了什么问题,但阅读起来越容易,就越容易找到错误!
在我发现错误后进行编辑 ha
这部分效果很好,是你想要的
if(number == doubleArray[index])
{
count++;
}
但如果这是在 for 循环中,
number = doubleArray[index + 1];
之后的每个数字都将被视为匹配。
我怎样才能改变它以便为每个值更新
如果您创建第二个数组来跟踪每个数字,您可以非常可靠地做到这一点,但我相信还有更有效的方法。
int[] digitCount = new int[11]; // 1.0 - 2.0
...
if(number == doubleArray[index])
{
digitCount[number]++;
}
...
然后在你的打印中
...
System.out.print("$".repeat(digitCount[i]));
...
确保i (或任何你称呼它的东西)是一个整数,因为你不能有十进制索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.