簡體   English   中英

我很難跳到循環的正確部分

[英]I'm having difficulty jumping to the correct section of a loop

我要做的是讓用戶輸入分子和分母,並顯示商。 我正在使用一種方法來檢查並使用Integer.parseInt和try catch查看該數字是否有效。 問題是如果try catch捕獲異常並顯示錯誤,它將繼續執行代碼的下一部分,而不是跳回到循環的開頭。

public static void main(String[] args) {
        String numerator = "";
        String denominator = "";
        String message = "";
        int num = 0;
        int den = 0;
        int quo = 0;
        int yesNo = 0;
        do
        {
            // Displays a JOptionPane asking the user to input a numerator.
            numerator = JOptionPane.showInputDialog(null, "Please enter a numerator.", "Enter a Numerator",JOptionPane.QUESTION_MESSAGE);
            if(numerator == null)// This breaks the loop if  the user clicks the "cancel" button.
                break;
            num = IsValid(numerator); // Calls the IsValid method, passing it the String entered by the user to validate if it is a proper integer.
            denominator = JOptionPane.showInputDialog(null, "Please enter a denominator.", "Enter a Denominator",JOptionPane.QUESTION_MESSAGE);
            if(denominator == null) // same as above but for the denominator
                break;
            den = IsValid(denominator);
            /*if(den != 0 && num<den) 
            { // This section is unrelated to my problem
                quo = num/den;
                message = "Numerator: " + num + "\n" +
                            "Denominator: " + den + "\n" +
                            "Quotient: ";
                JOptionPane.showMessageDialog(null, message,"Calculation",JOptionPane.INFORMATION_MESSAGE);
            }*/
            yesNo = JOptionPane.showConfirmDialog(null,"Would you like to make another calculation?","Continue?",JOptionPane.YES_NO_OPTION);
        }while(yesNo == JOptionPane.YES_OPTION);
    }
    public static int IsValid(String input)
    {
        int num = 0; // This method is passed the String that the JOptionPane receives and using Integer.parseInt to make sure it's an integer, and assigns it to int "num".
        try{num = Integer.parseInt(input);}
        catch(NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(null, "Error: Invalid number. Please enter a valid number.","Invalid Input",JOptionPane.WARNING_MESSAGE);
        }
        return num;
        // My main issue is that it continues with the code instead of jumping back to the JOptionPane to ask the user for input a second time.
    }

基本上,我想調用IsValid(分子)並將其分配給int“num”,如果執行IsValid的catch語句,則跳回到do循環的開頭,以便再次顯示JOptionPane。 如果為分母執行了catch語句,則跳回到那個而不是從頭開始。 我不能將JOptionPane放在方法中,因為該方法無法判斷我是否正在向它發送分子或分母。 我對java很新,我確信有更簡單的方法可以做到這一點,所以感謝任何幫助。 謝謝!

如果輸入無效,對isValid的調用應該中斷您的程序。 最自然的方法是讓它拋出異常。 然后你的程序捕獲異常並退出。

package so20190423;

import java.awt.HeadlessException;

import javax.swing.JOptionPane;

public class Snippet {
    public static void main(String[] args) {
            String numerator = "";
            String denominator = "";
            String message = "";
            int num = 0;
            int den = 0;
            int quo = 0;
            int yesNo = 0;
            try {
                do
                {
                    // Displays a JOptionPane asking the user to input a numerator.
                    numerator = JOptionPane.showInputDialog(null, "Please enter a numerator.", "Enter a Numerator",JOptionPane.QUESTION_MESSAGE);
                    if(numerator == null)// This breaks the loop if  the user clicks the "cancel" button.
                        break;
                    num = IsValid(numerator); // Calls the IsValid method, passing it the String entered by the user to validate if it is a proper integer.
                    denominator = JOptionPane.showInputDialog(null, "Please enter a denominator.", "Enter a Denominator",JOptionPane.QUESTION_MESSAGE);
                    if(denominator == null) // same as above but for the denominator
                        break;
                    den = IsValid(denominator);
                    /*if(den != 0 && num<den) 
                    { // This section is unrelated to my problem
                        quo = num/den;
                        message = "Numerator: " + num + "\n" +
                                    "Denominator: " + den + "\n" +
                                    "Quotient: ";
                        JOptionPane.showMessageDialog(null, message,"Calculation",JOptionPane.INFORMATION_MESSAGE);
                    }*/
                    yesNo = JOptionPane.showConfirmDialog(null,"Would you like to make another calculation?","Continue?",JOptionPane.YES_NO_OPTION);
                }while(yesNo == JOptionPane.YES_OPTION);
            }
            catch(NumberFormatException ex)
            {
                JOptionPane.showMessageDialog(null, "Error: Invalid number. Please enter a valid number.","Invalid Input",JOptionPane.WARNING_MESSAGE);
            }
        }
        public static int IsValid(String input) throws NumberFormatException
        {
            int num = 0; // This method is passed the String that the JOptionPane receives and using Integer.parseInt to make sure it's an integer, and assigns it to int "num".
            num = Integer.parseInt(input);
            return num;
            // My main issue is that it continues with the code instead of jumping back to the JOptionPane to ask the user for input a second time.
        }
}

你甚至可以在while循環中限制try / catch,在輸入失敗后恢復,然后再試一次!

package so20190423;

import java.awt.HeadlessException;

import javax.swing.JOptionPane;

public class Snippet {
    public static void main(String[] args) {
            String numerator = "";
            String denominator = "";
            String message = "";
            int num = 0;
            int den = 0;
            int quo = 0;
            int yesNo = 0;
                do
                {
                    try {
                    // Displays a JOptionPane asking the user to input a numerator.
                    numerator = JOptionPane.showInputDialog(null, "Please enter a numerator.", "Enter a Numerator",JOptionPane.QUESTION_MESSAGE);
                    if(numerator == null)// This breaks the loop if  the user clicks the "cancel" button.
                        break;
                    num = IsValid(numerator); // Calls the IsValid method, passing it the String entered by the user to validate if it is a proper integer.
                    denominator = JOptionPane.showInputDialog(null, "Please enter a denominator.", "Enter a Denominator",JOptionPane.QUESTION_MESSAGE);
                    if(denominator == null) // same as above but for the denominator
                        break;
                    den = IsValid(denominator);
                    /*if(den != 0 && num<den) 
                    { // This section is unrelated to my problem
                        quo = num/den;
                        message = "Numerator: " + num + "\n" +
                                    "Denominator: " + den + "\n" +
                                    "Quotient: ";
                        JOptionPane.showMessageDialog(null, message,"Calculation",JOptionPane.INFORMATION_MESSAGE);
                    }*/
                    yesNo = JOptionPane.showConfirmDialog(null,"Would you like to make another calculation?","Continue?",JOptionPane.YES_NO_OPTION);
                }
                catch(NumberFormatException ex)
                {
                    JOptionPane.showMessageDialog(null, "Error: Invalid number. Please enter a valid number.","Invalid Input",JOptionPane.WARNING_MESSAGE);
                }
                }while(yesNo == JOptionPane.YES_OPTION);
        }
        public static int IsValid(String input) throws NumberFormatException
        {
            int num = 0; // This method is passed the String that the JOptionPane receives and using Integer.parseInt to make sure it's an integer, and assigns it to int "num".
            num = Integer.parseInt(input);
            return num;
            // My main issue is that it continues with the code instead of jumping back to the JOptionPane to ask the user for input a second time.
        }
}


最后一個變體,如果要重試每個輸入,直到它有效(或由用戶取消):

package so20190423;

import javax.swing.JOptionPane;

public class Snippet {
    public static void main(String[] args) {
        String numerator = "";
        String denominator = "";
        String message = "";
        int num = 0;
        int den = 0;
        int quo = 0;
        int yesNo = 0;
        do {
            boolean userCancelled = false;
            boolean numeratorIsOk = false;
            do {
                try {
                    numerator = JOptionPane.showInputDialog(null, "Please enter a numerator.", "Enter a Numerator",
                            JOptionPane.QUESTION_MESSAGE);
                    if (numerator == null) {
                        userCancelled = true;
                    } else {
                        num = IsValid(numerator);
                        numeratorIsOk = true;
                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Error: Invalid number. Please enter a valid number.",
                            "Invalid Input", JOptionPane.WARNING_MESSAGE);
                }
            } while (!numeratorIsOk && !userCancelled);
            if (userCancelled) {
                break;
            }

            boolean denominatorIsOk = false;
            do {
                try {
                    denominator = JOptionPane.showInputDialog(null, "Please enter a denominator.",
                            "Enter a Denominator", JOptionPane.QUESTION_MESSAGE);
                    if (denominator == null) {
                        userCancelled = true;
                    } else {
                        den = IsValid(denominator);
                        denominatorIsOk = true;
                    }
                    /*
                     * if(den != 0 && num<den) { // This section is unrelated to my problem quo =
                     * num/den; message = "Numerator: " + num + "\n" + "Denominator: " + den + "\n"
                     * + "Quotient: "; JOptionPane.showMessageDialog(null,
                     * message,"Calculation",JOptionPane.INFORMATION_MESSAGE); }
                     */
                    yesNo = JOptionPane.showConfirmDialog(null, "Would you like to make another calculation?",
                            "Continue?", JOptionPane.YES_NO_OPTION);
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Error: Invalid number. Please enter a valid number.",
                            "Invalid Input", JOptionPane.WARNING_MESSAGE);
                }

            } while (!denominatorIsOk && !userCancelled);
            if (userCancelled) {
                break;
            }
        } while (yesNo == JOptionPane.YES_OPTION);

    }

    public static int IsValid(String input) throws NumberFormatException {
        int num = 0; // This method is passed the String that the JOptionPane receives and using
                        // Integer.parseInt to make sure it's an integer, and assigns it to int "num".
        num = Integer.parseInt(input);
        return num;
        // My main issue is that it continues with the code instead of jumping back to
        // the JOptionPane to ask the user for input a second time.
    }
}

HTH!

暫無
暫無

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

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