Java再玩一次还有错误吗?

时间:2014-11-26 00:37:45

标签: java if-statement intersection difference

我的程序应该从用户那里收到2组数字,找到两组之间的交集和差异,然后询问用户是否要再次播放。我有一个问题试图让我的程序再次播放,我无法弄清楚为什么我所知道的是我继续得到“令牌上的语法错误”否则“,删除此令牌”,我不知道为什么我一直得到这个错误,请帮助。

import java.util.*;

public class Sets { private static final int MAXSIZE = 20;// the maximum size of the array is 20 static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {
    int[] resultSet = new int[MAXSIZE];
    int[] setA = new int[MAXSIZE];
    int y=0;

    System.out.print("How many numbers will be in the first set? ");
    int sizeA = keyboard.nextInt();//enter the numbers for the first set
    while (sizeA < 1 || sizeA > MAXSIZE) {//a number larger than 20 can't be entered
        System.out.print("Size is not valid, please try again: ");
        sizeA = keyboard.nextInt();
    }
    getData(setA, sizeA);
    sort(setA,sizeA);

    printData(setA,sizeA);

    int[] setB = new int[MAXSIZE];
    System.out.print("How many numbers will be in the second set? ");
    int sizeB = keyboard.nextInt();//enter the numbers for the second set
    while (sizeB < 1 || sizeB > MAXSIZE) {
        System.out.print("Size is not valid, please try again: ");
        sizeB = keyboard.nextInt();
    }
    getData(setB, sizeB);//go to get getData method
    sort(setB,sizeB);
    System.out.println("The intersection is: ");
    int temp = intersection(setA,sizeA,setB,sizeB,resultSet);
    printData(resultSet, temp);
    System.out.println("The difference is: ");
    temp = difference(setA,sizeA,setB,sizeB,resultSet);
    printData(resultSet, temp);
    System.out.println("Do you want to repeat the program again with two different sets (y or n)?");
    int playAgain = keyboard.nextInt();
    if ("y" != null )       
        main(args);
}
else{
            break;
    }


public static void getData(int[] set, int size) {
    // This method will retrieve "size" number of unique integers from the
    // keyboard.
    // The integers will be placed into the array set.
    int temp, uniqueNum = 0;
    while (uniqueNum < size) {
        boolean flag = true;
        temp = keyboard.nextInt();

        for (int i = 0; i < uniqueNum; i++) {
            if (temp == set[i]) {
                flag = false;
                break;
            }
        }
        if (flag) {
            set[uniqueNum] = temp;
            uniqueNum++;

        }
    }

}

public static int intersection(int[] setA, int sizeA, int[] setB,
        int sizeB, int[] resultSet) {
    // This method will calculate the intersection of sets A and B.
    int found = 0;
    for (int x = 0; x < sizeA; x++) {
        for (int y = 0; y < sizeB; y++) {
            if (setA[x] == setB[y]) {
                resultSet[found] = setA[x];
                found++;
                break;
            }
        }
    }

    return found;
}

public static int difference(int[] setA, int sizeA, int[] setB, int sizeB,
        int[] resultSet) {
    // This method will calculate the difference (A-B) of the two sets.
    int found = 0;
    for (int x = 0; x < sizeA; x++) {
        boolean flag = true;
        for (int y = 0; y < sizeB; y++) {
            if (setA[x] == setB[y]) {
                flag = false;
                break;
            }
        }
        if (flag) {
            resultSet[found] = setA[x];
            found++;
        }

    }

    return found;
}

public static void sort(int[] nums, int size) {
    // This method will sort the elements in the nums array into ascending
    // order.
    for (int count = 0; count < size; count++) {
        int integer = count;
        int B = nums[count];
        while ((integer > 0) && (nums[integer - 1] > B)) {
            nums[integer] = nums[integer - 1];
            integer--;
        }
        nums[integer] = B;
    }
}

public static void printData(int[] set, int size) {
    for (int i = 0; i < size; i++)
        System.out.print(set[i] + " ");
    System.out.println("");

}

}

1 个答案:

答案 0 :(得分:1)

在if:

之后,您缺少左括号({
if ("y" != null )       

所以下面的结束括号实际上结束了方法

        main(args);
} // method ended here
else{
            break;
    }

并且你的else不是勒芒,编译器不喜欢。