Java:如果输入“ Y”,则对话框陷入循环,但如果输入“ N”,则对话框陷入循环

时间:2018-11-07 04:01:10

标签: java

程序要求输入一堆输入,然后转到询问用户是否要运行另一个报告的输入。问题是,当用户输入“ Y”时,它陷入无限循环。

如果用户输入“ N”,则它将更改runAnother变量并退出循环。我不明白为什么它陷入一个循环,当我希望它从顶部再次开始执行do while循环时,不断弹出“您想运行另一个报告”框。寻找任何建议或指示来帮助我解决此问题。

谢谢。

import javax.swing.JOptionPane;


public class SalesReportArrayConnorS {

    public static void main(String[] args) {

        //Declarartions

        int total = 0;
        int numProperties;
        int counter;
        int loopIndex;
        int propertyPrice[];
        int exception;
        String runAnother = "Y";
        String propertyAddress[];
        final String HEADING1 = "MONTH-END SALES REPORT";
        final String HEADING = "Address\t\t\tPrice";

        //First Box

        numProperties = Integer.parseInt(JOptionPane.showInputDialog("How many properties would you like to enter? "));

        //Output Headers

        System.out.println(HEADING1);
        System.out.println();
        System.out.println(HEADING);

        //Declare Array

        propertyAddress = new String[numProperties];
        propertyPrice = new int[numProperties];

        //While Loop

        counter = 1; //Makes sure counter on numProperties and prices starts with 1 instead of 0
        loopIndex = 0;

        do{
            while (loopIndex < numProperties){

                //Verifying numProperties is between 1 and 10
                if (numProperties > 10){
                    numProperties = Integer.parseInt(JOptionPane.showInputDialog("**ERROR: Enter a number between 1 and 10. \nHow many properties would you like to enter? "));
                }
                else if (numProperties < 1){
                    numProperties = Integer.parseInt(JOptionPane.showInputDialog("**ERROR: Enter a number between 1 and 10. \nHow many properties would you like to enter? "));
                }

                //Input propertyAddress and price in the arrays
                propertyAddress[loopIndex] = JOptionPane.showInputDialog("Enter the address for property " + counter + ": ");

                //Attempting to catch a NumberFormatException if a number is not entered
                exception = 1;
                do {
                    try {
                        propertyPrice[loopIndex] = Integer.parseInt(JOptionPane.showInputDialog("Enter the value of propery " + counter + ": "));
                        exception++;
                    }
                    catch(NumberFormatException nfe){
                        propertyPrice[loopIndex] = Integer.parseInt(JOptionPane.showInputDialog("**ERROR: Please enter a numeric value: "));
                        exception++;
                    } 
                } while (exception == 1);

                //Verifying property price is not 0
                if (propertyPrice[loopIndex] == 0){propertyPrice[loopIndex] = Integer.parseInt(JOptionPane.showInputDialog("**ERROR: Enter a number greater than 0. \nEnter the value of property " + counter + ": "));
                }

                //Add one to counters
                counter++;
                loopIndex++;    
            }

            runAnother = JOptionPane.showInputDialog("Would you like to run another report? ");

        } while (runAnother.equalsIgnoreCase("Y"));




        //loop to print out address, price arrays, and calculate total.

        for (loopIndex = 0; loopIndex < numProperties; loopIndex++){
            total += propertyPrice[loopIndex];
            System.out.println(propertyAddress[loopIndex] + "\t\t" + propertyPrice[loopIndex]);

        }

        //Print total
        System.out.println();
        System.out.println("Total\t\t\t" + total);

        //Exit System
        System.exit(0);

    }

}

1 个答案:

答案 0 :(得分:1)

由于loopIndex未重置,因此

while (loopIndex < numProperties){

循环永远不会进入

之后(或之前)

runAnother = JOptionPane.showInputDialog("Would you like to run another report? ");

loopIndex设置为0