寻找唯一的数字

时间:2017-04-04 01:27:30

标签: java

我正在尝试使用一个集合来查找唯一的数字,并从用户输入的数字中获取总和。我听说数组更容易,但一组可能只对我有用。我不太了解集合或他们做什么,所以任何输入都会很棒。非常感谢所有人!

@pytest.mark.usefixtures("fixture1", "fixture2", "fixture3")
class TestFunction:
    .........

3 个答案:

答案 0 :(得分:2)

java.util.HashSet存储唯一值。对程序进行微小更改以使用Set来存储唯一值并使用for循环计算唯一值的总和

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class quiz_assignment {
int counter;
Scanner scan = new Scanner(System.in);

public int go() {
    int a = 0;
    int n = 0;
    counter = 10;

    Set<Integer> unValues = new HashSet<Integer>();

    while (counter != 0) {
        System.out.println("Enter Integer: ");
        int newInteger = scan.nextInt();
        if (newInteger < 0) {
            unValues.add(new Integer(newInteger));
            n += newInteger;
            counter = counter - 1;
            a = a + newInteger;
        } else {
            System.out
                    .println("Must be negative integer, please try again");
        }
    }

    int unSum = 0;
    for (Integer value : unValues) {
        unSum += value;
    }

  System.out.println("The sum of all ten integers is: " + a);
    System.out.println("The sum of unique integers is: " + unSum);
    return n;
}

public static void main(String[] args) {
    quiz_assignment o = new quiz_assignment();
    o.go();
}
}

答案 1 :(得分:0)

Set在HashSet,TreeSet,LinkedHashSet等各种类中都有实现。以下是解释Set功能的示例 我在这里使用(HashSet)。

import java.util.Scanner;

public class quiz_assignment 
{ 
   int counter;
   Scanner scan = new Scanner(System.in);
   Set<Integer> ditinctSet = new HashSet<Integer>();

   public int go()
   {
      int a=0;
      int n=0;
      counter = 10;

      int total = 0;

      while (counter != 0)
      {
         System.out.println("Enter Integer: ");
         int newInteger = scan.nextInt();

         if(!ditinctSet.contains(newInteger)){
              ditinctSet.add(newInteger);
         }

             if(newInteger < 0) 
             {
                 n+=newInteger;
                 counter = counter - 1;
                 a=a+newInteger;
             }
             else 
             {
                 System.out.println("Must be negative integer, please try again");
             }
      } 

      System.out.println("The sum of all ten integers is: " + a);
      System.out.println("Distinct numbers are: ");
      System.out.println(ditinctSet);
      return n;
   }
}

这里有一个关于开始更多关于集合的链接。

答案 2 :(得分:0)

Hashsets非常有用,因为它们不会存储重复的条目。您可以使用它们来存储用户输入的唯一数字集。我还删除了变量&#34; a&#34;来自你的代码,因为它的目的似乎与变量n相同。

import java.util.Scanner;

public class quiz_assignment{ 
int counter;
Scanner scan = new Scanner(System.in);

public int go()
{
  HashSet<Integer> distinctNumbers = new HashSet<>();
  int n=0;
  counter = 10;

  int total = 0;

  while (counter != 0)
  {
     System.out.println("Enter Integer: ");
     int newInteger = scan.nextInt();
         if(newInteger < 0) 
         {
             n+=newInteger;
             counter = counter - 1;
             distinctNumbers.add(newInteger);
         }
         else 
         {
             System.out.println("Must be negative integer, please try again");
         }
  } 
  int size = distinctNumbers.size();

  System.out.println("The sum of all ten integers is: " + n);

  System.out.println("You inputed " + size + " numbers.");

  System.out.println("Your numbers are:");

  for(Integer i: distinctNumbers){
       System.out.println(i);
  }



  return n;
 }
}