[英]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.