这个布尔循环不断重复?

时间:2015-03-21 23:38:42

标签: java loops while-loop boolean

对于下面显示的这段代码,我需要它循环回到已经存在的开头,如果有人没有输入double,但是在它进入程序的下一部分后,循环仍然是去。我该如何解决这个问题?

public static void cat()
{
    boolean loop = true;
    while (loop)
    {
        //read in name from user as a string
        // read in first number from user as a string
        name1 = JOptionPane.showInputDialog ("Enter the model of the first car.");
        firstNumber = JOptionPane.showInputDialog ("Enter the price of the first car.");
        repair1 = JOptionPane.showInputDialog ("Enter the price of the repairs for the first car.");

        // read in second number from user as a string
        name2 = JOptionPane.showInputDialog ("Enter the model of the second car.");
        secondNumber = JOptionPane.showInputDialog ("Enter the price of the second car.");
        repair2 = JOptionPane.showInputDialog ("Enter the price of the repairs for the second car.");

        // read in third number from user as a string
        name3 = JOptionPane.showInputDialog ("Enter the model of the third car.");
        thirdNumber = JOptionPane.showInputDialog ("Enter the price of the third car.");
        repair3 = JOptionPane.showInputDialog ("Enter the price of the repairs for the third car.");

        // read in forth number from user as a string        
        check();
    } // convert numbers from type String to type int
}

public static void check()
{
    try
    {
        number1 = Double.parseDouble (firstNumber);
        number2 = Double.parseDouble (secondNumber);
        number3 = Double.parseDouble (thirdNumber);
        Repair1 = Double.parseDouble (repair1);
        Repair2 = Double.parseDouble (repair2);
        Repair3 = Double.parseDouble (repair3);
        tax1 = number1 * 0.13;
        tax2 = number2 * 0.13;
        tax3 = number3 * 0.13;
        total1 = number1 + tax1 - Repair1;
        total2 = number2 + tax2 - Repair2;
        total3 = number3 + tax3 - Repair3;
        tc1 = Double.toString (total1);
        tc2 = Double.toString (total2);
        tc3 = Double.toString (total3);

        // display the results
        Chapter4 mainFrame = new Chapter4 ();
        mainFrame.setVisible (true);

        //System.exit (0);  // terminate the program
    }
    catch (NumberFormatException e)
    {
        JOptionPane.showMessageDialog (null,
                "Error: You have to enter a car price!",
                "Error Message", JOptionPane.ERROR_MESSAGE);
    } //end try/catch block
}

3 个答案:

答案 0 :(得分:4)

您应该更改loop值,否则while循环将无限重复。

check返回boolean并在cat内循环,直到check返回true

public static void cat() 
{
    boolean loop = true;

    while (loop) 
    {
        //read in name from user as a string
        // read in first number from user as a string
        name1 = JOptionPane.showInputDialog("Enter the model of the first car.");
        firstNumber = JOptionPane.showInputDialog("Enter the price of the first car.");
        repair1 = JOptionPane.showInputDialog("Enter the price of the repairs for the first car.");

        // read in second number from user as a string
        name2 = JOptionPane.showInputDialog("Enter the model of the second car.");
        secondNumber = JOptionPane.showInputDialog("Enter the price of the second car.");
        repair2 = JOptionPane.showInputDialog("Enter the price of the repairs for the second car.");

        // read in third number from user as a string
        name3 = JOptionPane.showInputDialog("Enter the model of the third car.");
        thirdNumber = JOptionPane.showInputDialog("Enter the price of the third car.");
        repair3 = JOptionPane.showInputDialog("Enter the price of the repairs for the third car.");

        // read in forth number from user as a string
        loop = !check();
    } // convert numbers from type String to type int
}

public static boolean check() 
{
    try 
    {
        number1 = Double.parseDouble(firstNumber);
        number2 = Double.parseDouble(secondNumber);
        number3 = Double.parseDouble(thirdNumber);
        Repair1 = Double.parseDouble(repair1);
        Repair2 = Double.parseDouble(repair2);
        Repair3 = Double.parseDouble(repair3);
        tax1 = number1 * 0.13;
        tax2 = number2 * 0.13;
        tax3 = number3 * 0.13;
        total1 = number1 + tax1 - Repair1;
        total2 = number2 + tax2 - Repair2;
        total3 = number3 + tax3 - Repair3;
        tc1 = Double.toString(total1);
        tc2 = Double.toString(total2);
        tc3 = Double.toString(total3);

        // display the results
        Chapter4 mainFrame = new Chapter4();
        mainFrame.setVisible(true);

        //System.exit (0);  // terminate the program
        return true;
    } 
    catch (NumberFormatException e) 
    {
        JOptionPane.showMessageDialog(null,
            "Error: You have to enter a car price!",
            "Error Message", JOptionPane.ERROR_MESSAGE);
    } //end try/catch block
    return false;
}

或者@pbabcdefp建议,将其设为do..while循环。它是一个完美的用例,您可以完全避免使用loop变量:

public static void cat() 
{
    do 
    {
        //read in name from user as a string
        // read in first number from user as a string
        name1 = JOptionPane.showInputDialog("Enter the model of the first car.");
        firstNumber = JOptionPane.showInputDialog("Enter the price of the first car.");
        repair1 = JOptionPane.showInputDialog("Enter the price of the repairs for the first car.");

        // read in second number from user as a string
        name2 = JOptionPane.showInputDialog("Enter the model of the second car.");
        secondNumber = JOptionPane.showInputDialog("Enter the price of the second car.");
        repair2 = JOptionPane.showInputDialog("Enter the price of the repairs for the second car.");

        // read in third number from user as a string
        name3 = JOptionPane.showInputDialog("Enter the model of the third car.");
        thirdNumber = JOptionPane.showInputDialog("Enter the price of the third car.");
        repair3 = JOptionPane.showInputDialog("Enter the price of the repairs for the third car.");

        // read in forth number from user as a string
    } while(!check()); // convert numbers from type String to type int
}

答案 1 :(得分:1)

您没有更新循环变量。添加代码来更新可以解决您的问题,这是另一种方法。更改代码,以便您的check()函数抛出异常而不是捕获它。然后,在cat()函数中捕获它以退出循环。

例如:

public static void check() throws NumberFormatException {
    //code here, get rid of the try block inside
}

同时更新主cat()

public static void cat() {
    try {
        while (loop) {
            // other code here
            check();
        }
    } catch (NumberFormatException e) {
        // handle it
    }
}

如果你这样做,你也可以摆脱循环变量并将其改为while (true)

答案 2 :(得分:1)

你的问题很难解释,但我会给它一个摆动。

你根据变量"循环"进行循环。这等于真的。为了摆脱循环,你需要在某些时候将循环的值更改为false