在周围循环,没有在哪里:(

时间:2014-10-23 20:59:18

标签: java loops while-loop

我试着在循环中制作循环,有点像电影Inception。但它弄乱了一些方法,当我进入" N"时,第二个循环不会停止。为了不,并且不会回到第一个循环,这是"继续另一个集合?",当我输入" Y"是的。相反,它会继续回到"要检查什么值?"无论如何:(

while (true){
    // 2. Ask the user for size of arbitrary numbers.
    System.out.print("Please enter a size for arbitray numbers: ");
    int size = indata.nextInt(); 
    int [] SortedNumbers = new int [size];

    // 3. Process arbitrary numbers and remove all duplicates
    int numDuplicates = generate_data(SortedNumbers);

    // 4. Print the numbers and number of duplicates
    printArray(SortedNumbers, numDuplicates);

    while (true){
        // 5. Ask user for target value
        System.out.print("\nWhat target value to check? ");
        int target = indata.nextInt();

        // 6. Perform linear and binary search
        int linearVal = linear_search(SortedNumbers,target);
        int binaryVal = binary_search(SortedNumbers,target);
        if (linearVal == -1)
            System.out.println("Linear search: " + target + " does not exist in the array.");
        else 
            System.out.println("Linear search: " +target + " is in location " + linearVal);

        if (binaryVal == -1)
            System.out.println("Binary search: " + target + " does not exist in the array.");
        else 
            System.out.println("Binary search: " +target + " is in location " + binaryVal);

        // 7. Repeat steps 5 and 6 until the user is satisfied.
        System.out.println("Continue? Y/N");
        String answer = indata.next();
        if (answer=="Y"){
            break;
        }
    }
    // 8. Repeat steps 2-7 until the user is done working with different arbitrary sets of numbers.
    System.out.println("Continue with another set? Y/N");
    String answer2 = indata.next();
    if (answer2=="Y"){
        break;
    }
}

所以从技术上讲,我需要它在我进入N时停止,或者问我是否需要"一套新的"当我输入Y,然后在第二个循环中返回"值以检查"在N上,或从Y开始。

0 个答案:

没有答案