簡體   English   中英

java 中的循環和用戶輸入驗證,再次

[英]Loops and user input validation in java, again

所以,我想兩天前我問了一個關於輸入驗證的問題,以及如何循環程序直到用戶給出有效的輸入..

所以我做了一個計算器,我想循環程序的每一步,這樣如果用戶沒有輸入double (69, 42.0) 或適當的運算符char (/, *, +, -) 他們就會卡在這一步,直到他們做對或以其他方式完全關閉應用程序。

所以我從最后一個問題中得到的一件事是,我可以制作一個名為“restart”的boolean值或其他東西,並封裝我的整個代碼,除了main method ,最后我可以提出一個他們可以回答true問題或false並且整個應用程序可以再次運行。 雖然我承認在我的應用程序上有一個“重新開始”按鈕很酷,對我所有其他項目都很有用(可能不是,我相信這可以更有效地完成),但它仍然沒有讓我和我原來的問題感到滿足.

所以...

我必須盡我所能(我知道堆棧喜歡那些表明他們至少嘗試過的人)。

例 1

Scanner input = new Scanner(System.in);

        double valX;
        
            System.out.println("Calculator Activated.");

                do
                {
                    System.out.print("Value X: ");
                    valX = input.nextDouble();
                }while (!input.hasNextDouble());
                {
                    System.out.println("Invalid number!");
                }
//Didn't work ¯\_(ツ)_/¯

例 2

Scanner input = new Scanner(System.in);

        double valX;

            System.out.println("Calculator Activated.");

            while (!input.hasNextDouble())
            {
                System.out.println("Value X: ");
                valX = input.nextDouble();
            }
//neither this one...

請注意,你們給我的解決方案必須適用於程序的每一步。

為了更清楚一點,整個 src 代碼沒有我嘗試過的東西和“重啟”循環。

import java.util.Scanner;

public class CalTest
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        double valX, valY, divided, multiplied, added, subtracted;  //Declared all my variables...
        char operator;
        boolean restart; //Didn't need to declare it true or false since I'm using a Scanner anyway

        do //Start of the entire program
        {
            System.out.println("Calculator Activated.");

            System.out.print("Value X: "); //I need a loop here...
            valX = input.nextDouble();
            
            System.out.print("Operator: "); //And here...
            operator = input.next().charAt(0);

            System.out.print("Value Y: "); //And here too..
            valY = input.nextDouble();

            divided = valX / valY;
            multiplied = valX * valY;
            added = valX + valY;
            subtracted = valX - valY;

            if (operator == '/')
                System.out.println("Result: " + divided );
            else if (operator == '*')
                System.out.println("Result: " + multiplied); //<--Not sure if I need a loop with the if's
            else if (operator == '+')
                System.out.println("Result: " + added);
            else if (operator == '-')
                System.out.println("Result: " + subtracted);
            else
                System.out.println("Invalid operator!");

            System.out.print("Try again? "); //I also need a loop here, I think.
            restart = input.nextBoolean();

        } while (restart); //End of it if you declared false.
        
        System.out.println("Calculator terminated.");
    }
}

有一次,我嘗試使用相同的“重新啟動應用程序”概念,並為代碼中的每一步創建了一個 boolean 變量,老實說,這很煩人,不值得。

另外,我只是一個初學者,如果它是我所缺少的循環的概念,那么我很高興向你們學習。

再次感謝任何回答並幫助我學習的人。

在名為CalTest的 class 的最終代碼示例中,您分配valX = input.nextDouble(); 你可以調用遞歸方法來處理異常,直到輸入是你想要的。 像這樣的東西:

private static double getNextDouble(Scanner input) {
    try {
        return input.nextDouble();
    } catch (InputMismatchException e) {
        System.out.println("Value X must be a number: ");
        input.next();
        return getNextDouble(input);
    }
}

您將替換valX = input.nextDouble(); with valX = getNextDouble(input); .

您可以整理它並使其適用於您的其他潛在錯誤情況,也許為 output 消息創建一個參數並將其作為參數傳遞。

public static void main(String[] args) {
    double valX=0,valY=0;
    char operator='0';//dummy default

    Scanner input = new Scanner(System.in);
    System.out.println("Calculator Activated.");

    Double dX = null;
    do
    {
        System.out.print("Value X: ");
        dX = getDouble(input.next());
    }while (dX==null);
    {
        valX = dX;
    }

    Character op = null;
    do
    {
        System.out.print("Operator: ");
        op = getOperator(input.next());
    }while (op==null);
    {
        operator=op;
    }


    Double dY = null;
    do
    {
        System.out.print("Value Y: ");
        dY = getDouble(input.next());
    }while (dY==null);
    {
        valY = dY;
    }


    System.out.println("Done: "+ valX + " "+operator + " " +valY);
}

static Double getDouble(String input) {
    Double d = null;
    try {
        d = new Double(input);
    }catch(NumberFormatException ex){
    }
    return d;
}

static Character getOperator(String input) {
    Character c = null;
    if("+".equals(input) || "-".equals(input) || "*".equals(input) || "/".equals(input)){
        c = input.charAt(0);
    }
    return c;
}

暫無
暫無

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

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