Lonely Integer - 输出正确。但仍然得到额外的消息

时间:2015-02-19 18:43:59

标签: java

我试图在数组中找到孤独的整数。我的输出是正确的,但仍然得到额外的消息。请看一下代码。我正在使用Java编写程序。

代码:

import java.util.InputMismatchException;
import java.util.Scanner;

public class LonelyInteger {

    private static int inputArray[];
    private static int inputLength;
    private static final Scanner scanner = new Scanner(System.in);;

    public static void main(String[] args) {

        try {
            if (getInput()) {
                sortAndPrintArray();
                findLonelyInteger();
            } else {
                System.out.println("OOPS, something is not right! Try again!");
            }
        } catch (NumberFormatException | InputMismatchException nfime) {
            System.out.print("Number Format Exception or Input Mismatch Exception Occured: " + nfime);
        } catch (Exception e) {
            System.out.print("Exception Occured: " + e.getMessage());
        }

    }

    private static boolean getInput() throws NumberFormatException, InputMismatchException, Exception {

        System.out.print("Enter the array length: ");
        inputLength = scanner.nextInt();
        if (inputLength <= 0) {
            return false;
        }
        inputArray = new int[inputLength];
        System.out.println("Enter the array:");
        for (int i = 0; i < inputLength; i++) {
            inputArray[i] = scanner.nextInt();
        }
        return true;
    }

    private static void sortAndPrintArray() {
        sortArray();
        printSortedArray();
    }

    private static void sortArray() {
        int temp = 0;
        for (int i = 0; i < inputLength; i++) {
            for (int j = 0; j < i; j++) {
                if (inputArray[i] < inputArray[j]) {
                    temp = inputArray[i];
                    inputArray[i] = inputArray[j];
                    inputArray[j] = temp;
                }
            }
        }
    }

    private static void printSortedArray() {
        System.out.println("Sorted Array:");
        for (int i = 0; i < inputLength; i++) {
            System.out.print(inputArray[i] + " ");
        }
        System.out.println();
    }

    private static void findLonelyInteger() {
        boolean foundLonelyInteger = false;
        for (int i = 0; i < inputLength; i++) {
            if ((i+1) == inputLength) {
                System.out.println("Lonely Integer: " + inputArray[i]);
                break;
            }
            if (inputArray[i] == inputArray[++i]) {
                continue;
            } else {
                System.out.println("Lonely Integer: " + inputArray[i-1]);
                foundLonelyInteger = true;
                i--;
            }
        }
        if (!foundLonelyInteger) {
            System.out.println("Lonely integer not available!");
        }
    }

}

这是我的输出,可在命令提示符中看到:

输出:

Enter the array length: 5
Enter the array:
1
2
2
1
2
Sorted Array:
1 1 2 2 2 
Lonely Integer: 2
Lonely integer not available!

2 个答案:

答案 0 :(得分:3)

您没有在findLonelyInteger()方法的第一if条件中设置标记!

if ((i+1) == inputLength) {
    System.out.println("Lonely Integer: " + inputArray[i]);
    foundLonelyInteger = true;  // --> HERE
    break;
}

命令提示符?开始使用Eclipse!并学习调试!

答案 1 :(得分:2)

在检查foundLonelyInteger = true;

时设置if((i+1) == inputLength)