簡體   English   中英

在循環中檢查和使用兩個數組

[英]Checking and using two arrays in loops

有什么方法可以簡化以下代碼。 粗略設置的方式是掃描值,但是如果輸入引發異常,則需要說出nonono並重​​新搜索該值。 我需要像這樣收集x和y值,然后才能以科學的計算器方式對其進行操作。 輸入字符串“ RESULT” =先前計算的答案是必要條件。 這兩個要求x和y的循環是如此相似,只有“第一個操作數”和“ x =答案”,而“第二個操作數”和“ y =答案”不同。 那么,有什么方法可以優化此代碼,以便僅需一個循環,因為兩者是如此相似? 這是代碼。

    String operand;
    double x = 0;
    double y = 0;

    //These two arrays are the differences between both of the loops that follow. Everything besides first, x and second, y are the same
    String arr[] = {"first", "second"};
    Double var[] = {x, y};

    boolean operandLoop1 = false;
    //x
    while (!operandLoop1) {
        System.out.print("Enter " + arr[0] + " operand: ");
        operand = calcOption.next(); // retrieve first value
        if (operand.equals("RESULT")) {
            var[0] = answer; // If I want to use the previous result as my input
            operandLoop1 = true;
        } else {
            try {
                var[0] = Double.parseDouble(operand); // Assumes that if it isn't RESULT, then I'll want to put in a number
                operandLoop1 = true;
            } catch (NumberFormatException nfe) { // necessary if I type anything else in besides RESULT and a double
                System.out.print("Error: Invalid input! Correct inputs are any real number and \"RESULT\"!");
            }
        }
    }

    boolean operandLoop2 = false;
    //y
    while (!operandLoop2) {
        System.out.print("Enter" + arr[1] + " operand: ");
        operand = calcOption.next(); // retrieve second value
        if (operand.equals("RESULT")) {
            var[1] = answer; // If I want to use the previous result as my input
            operandLoop2 = true;
        } else {
            try {
                var[1] = Double.parseDouble(operand); // Assumes that if it isn't RESULT, then I'll want to put in a number
                operandLoop2 = true;
            } catch (NumberFormatException nfe) { // necessary if I type anything else in besides RESULT and a double
                System.out.print("Error: Invalid input! Correct inputs are any real number and \"RESULT\"!");
            }
        }
    }

關於長度,我很抱歉,但是希望我能得到大約一半的長度。

由於這兩個部分之間唯一的區別是數組索引,因此可以使用for循環,如下所示:

for (int i = 0; i < 2; i++) {
    boolean operandLoop = false;
    while (!operandLoop) {
        System.out.print("Enter " + arr[i] + " operand: ");
        operand = calcOption.next(); // retrieve value
        if (operand.equals("RESULT")) {
            var[i] = answer; // If I want to use the previous result as my input
            operandLoop = true;
        } else {
            try {
                var[i] = Double.parseDouble(operand); // Assumes that if it isn't RESULT, then I'll want to put in a number
                operandLoop = true;
            } catch (NumberFormatException nfe) { // necessary if I type anything else in besides RESULT and a double
                System.out.print("Error: Invalid input! Correct inputs are any real number and \"RESULT\"!");
            }
        }
    }
}

您也可以使其成為一種方法,傳入calcOptionanswer和序數( arr[0] )的參數,並用return語句替換var[0]所有賦值。 我不知道calcOption的類型,但是看起來像這樣:

Double methodName(Object calcOption, Double answer, String ordinal) {
    boolean operandLoop = false;
    while (!operandLoop) {
        System.out.print("Enter " + ordinal + " operand: ");
        String operand = calcOption.next(); // retrieve value
        if (operand.equals("RESULT")) {
            return answer; // If I want to use the previous result as my input
        } else {
            try {
                return Double.parseDouble(operand); // Assumes that if it isn't RESULT, then I'll want to put in a number
            } catch (NumberFormatException nfe) { // necessary if I type anything else in besides RESULT and a double
                throw new RuntimeException("Error: Invalid input! Correct inputs are any real number and \"RESULT\"!");
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM