首页 文章

使用Java设置操作计算器 - 计算器部分不起作用

提问于
浏览
0

我在创建这个设置计算器时遇到了麻烦,它在某一点上工作正常,但我不知何故搞砸了它现在它实际上并没有找到联盟,交集,差异和补充 . 我的n只打印1而不是他们输入的集合 .

有人可以帮帮我吗?我可能做了一些非常关闭的事情:(谢谢 .

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

public class FinalProject4 {

    public static void main(String args[]) {
        String[] list;

        Scanner sc = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);


        Set<Integer> A = new HashSet<Integer>();
        System.out.println("Enter set A: ");
        A.addAll(Arrays.asList());
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println("You entered " + n);
        }


        Set<Integer> B = new HashSet<Integer>();
        System.out.println("Enter set B: ");
        B.addAll(Arrays.asList());
        int v = sc2.nextInt();
        for (int i = 0; i < v; i++) {
            System.out.println("You entered: " + v);
        }


        // To find union
        Set<Integer> union = new HashSet<Integer>();
        union.addAll(A);
        union.addAll(B);
        System.out.print("Union of the two Sets is: ");
        System.out.println(union);

        // To find intersection
        Set<Integer> intersection = new HashSet<Integer>();
        intersection.addAll(A);
        intersection.retainAll(B);
        System.out.print("Intersection of the two Sets is: ");
        System.out.println(intersection);

        // To find the difference
        Set<Integer> difference = new HashSet<Integer>();
        intersection.addAll(A);
        difference.removeAll(B);
        System.out.print("Difference of the two Sets is: ");
        System.out.println(difference);

        // To find the complement
        Set<Integer> complement = new HashSet<Integer>();
        complement.addAll(B);
        complement.removeAll(A);
        System.out.print("Complement of the two Sets is:");
        System.out.println(complement);

    }

}

1 回答

  • 0

    缺少的部分是为初始集 AB 添加值的方法,您可以请求多个值,然后询问值 . 也不要使用2个不同的 Scanners 只使用一个

    Set<Integer> A = new HashSet<>();
    System.out.print("Enter set A, How many values do you want in it ? ");
    int n = sc.nextInt();
    for (int i = 0; i < n; i++) {
        System.out.print("Write a value to add : ");
        A.add(sc.nextInt());
    }
    
    
    Set<Integer> B = new HashSet<>();
    System.out.print("Enter set B, How many values do you want in it ? ");
    int v = sc.nextInt();
    for (int i = 0; i < v; i++) {
        System.out.print("Write a value to add : ");
        B.add(sc.nextInt());
    }
    

    difference 部分中有一个拼写错误:

    • 你应该 difference.addAll(A); 而不是 intersection.addAll(A);

    你会有:

    Enter set A, How many values do you want in it ? 5
    Write a value to add : 1
    Write a value to add : 2
    Write a value to add : 3
    Write a value to add : 4
    Write a value to add : 5
    Enter set B, How many values do you want in it ? 5
    Write a value to add : 4
    Write a value to add : 5
    Write a value to add : 6
    Write a value to add : 7
    Write a value to add : 8
    Union of the two Sets is: [1, 2, 3, 4, 5, 6, 7, 8]
    Intersection of the two Sets is: [4, 5]
    Difference of the two Sets is: [1, 2, 3]
    Complement of the two Sets is:[6, 7, 8]
    

    Tip 为大家4操作,你可以简化

    Set<Integer> union = new HashSet<Integer>();
    union.addAll(A);
    
    // into 
    
    Set<Integer> union = new HashSet<Integer>(A);
    

相关问题