我再次坚持答案。此程序打印唯一值,但我无法正确获取这些唯一值的总和。任何帮助表示赞赏
public static void main(String args[]){
int sum = 0;
Integer[] numbers = {1,2,23,43,23,56,7,9,11,12,12,67,54,23,56,54,43,2,1,19};
Set<Integer> setUniqueNumbers = new LinkedHashSet<Integer>();
for (int x : numbers) {
setUniqueNumbers.add(x);
}
for (Integer x : setUniqueNumbers) {
System.out.println(x);
for (int i=0; i<=x; i++){
sum += i;
}
}
System.out.println(sum);
}
答案 0 :(得分:4)
这是使用Java 8语言添加的一个很好的例子:
int sum = Arrays.stream(numbers).distinct().collect(Collectors.summingInt(Integer::intValue));
此行将从Set
声明开始替换代码中的所有内容,直到System.out.println
之前的最后一行。
答案 1 :(得分:2)
不需要这个循环
for (int i=0; i<=x; i++){
sum += i;
}
因为您正在添加i
而不是集合中的实际整数。这里发生的是您将所有数字从0添加到x到sum
。因此,对于23
,您不会将sum
增加23,而是将{+ 1 + 3 + 4 + 5 + .... + 23添加到{{ 1}}。您需要做的就是添加x,因此上面的循环可以省略,并替换为将{x}添加到sum
的简单行,
sum
答案 2 :(得分:1)
如果在低电平环路等中发现错误,则总是会发生这种错误。 最好的是,摆脱低级代码并使用Java 8 API:
Integer[] numbers = {1,2,23,43,23,56,7,9,11,12,12,67,54,23,56,54,43,2,1,19};
int sum = Arrays.stream(numbers)
.distinct()
.mapToInt(Integer::intValue)
.sum();
通过这种方式几乎没有任何错误的空间。 如果你有一个int数组,代码甚至更短:
int[] intnumbers = {1,2,23,43,23,56,7,9,11,12,12,67,54,23,56,54,43,2,1,19};
int sumofints = Arrays.stream(intnumbers)
.distinct()
.sum();
答案 3 :(得分:0)
所以这是我第一次在任何地方发表评论,我真的很想分享只打印数组中唯一值而不需要任何实用程序的方式。
//The following program seeks to process an array to remove all duplicate integers.
//The method prints the array before and after removing any duplicates
public class NoDups
{
//we use a void static void method as I wanted to print out the array without any duplicates. Doing it like this negates the need for any additional code after calling the method
static void printNoDups(int array[])
{ //Below prints out the array before any processing takes place
System.out.println("The array before any processing took place is: ");
System.out.print("{");
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i]);
if (i != array.length - 1)
System.out.print(", ");
}
System.out.print("}");
System.out.println("");
//the if and if else statements below checks if the array contains more than 1 value as there can be no duplicates if this is the case
if (array.length==0)
System.out.println("That array has a length of 0.");
else if (array.length==1)
System.out.println("That array only has one value: " + array[0]);
else //This is where the fun begins
{
System.out.println("Processed Array is: ");
System.out.print( "{" + array[0]);//we print out the first value as it will always be printed (no duplicates has occured before it)
for (int i = 1; i < array.length; i++) //This parent for loop increments once the all the checks below are run
{
int check = 0;//this variable tracks the amount of times an value has appeared
for(int h = 0; h < i; h++) //This loop checks the current value for array[i] against all values before it
{
if (array[i] == array[h])
{
++check; //if any values match during this loop, the check value increments
}
}
if (check != 1) //only duplicates can result in a check value other than 1
{
System.out.print(", " + array[i]);
}
}
}
System.out.print("}"); //formatting
System.out.println("");
}
public static void main(String[] args)
{ //I really wanted to be able to request an input from the user but so that they could just copy and paste the whole array in as an input.
//I'm sure this can be done by splitting the input on "," or " " and then using a for loop to add them to the array but I dont want to spend too much time on this as there are still many tasks to get through!
//Will come back and revisit to add this if I remember.
int inpArray[] = {20,100,10,80,70,1,0,-1,2,10,15,300,7,6,2,18,19,21,9,0}; //This is just a test array
printNoDups(inpArray);
}
}
答案 4 :(得分:-1)
错误就行了
sum += i;
应该是
sum += x;