首页 > 解决方案 > Java 新手:Do While 循环和异常处理

问题描述

我正在尝试为一个介绍性的 java 类编写一个程序,该类接受墙的宽度和高度,计算面积,然后输出该墙所需的油漆量。如果用户要输入正确的值,我的代码工作得很好,但是我的 try、throw、catch 语句有问题。他们成功地抛出并捕获了错误,但如果发现错误并且我的 do while 循环似乎不起作用,我希望它提示用户输入另一个输入。任何帮助,将不胜感激。

/************************************************************************
 * This program is designed to take the area and width of a user given 
 * wall and output number of gallons of paint required.
 * Written by: Nicholas Phelps
 * Date: August 4, 2021
 ************************************************************************/

import java.util.Scanner;

public class Paint1 {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        double wallHeight = 0.0;
        double wallWidth = 0.0;
        double wallArea = 0.0;
        double gallonsPaintNeeded = 0.0;
        
        final double squareFeetPerGallons = 350.0;
        
        
        /************************************************************************************
         * Created a do while loop that utilizes try, throw, catch statements for error
         * checking. If 'wallHeight' is less than 0.0 then the error message will print and 
         * continues until a valid entry has been made.
         ***********************************************************************************/
        do {
            System.out.println("Enter wall height (feet): ");
            try {
                //Get user data
                wallHeight = scnr.nextDouble();
                
                //Error checking, non-negative height
                if(wallHeight < 0.0) {
                    throw new Exception("Invalid height.");
                }
            }
            catch(Exception excpt) {
                //Prints error message
                System.out.println(excpt.getMessage());
                System.out.println("Cannont calculate required paint. Please try again.");
            }
        }
        while(wallHeight < 0.0);
        

        /***********************************************************************************
         * Correction was made here. 'wallHeight' was used twice, changed to 'wallWidth'.
         * Created a do while loop that utilizes try, throw, catch statements for error 
         * checking. If 'wallWidth' is less than 0.0, then the error message will print and
         * loop continues until valid value has been entered. 
         ***********************************************************************************/
        do {
            System.out.println("Enter wall width (feet): ");
            try {
                //Get user data
                wallWidth = scnr.nextDouble();
                
                //Error checking, non-negative width
                if(wallWidth < 0.0) {
                    throw new Exception("Invalid width.");
                }
            }
            catch(Exception excpt) {
                //Prints error message
                System.out.println(excpt.getMessage());
                System.out.println("Cannont calculate required paint. Please try again.");
            }
        }
        while(wallWidth < 0.0);
        

        // Calculate and output wall area
        wallArea = wallHeight * wallWidth;
        System.out.println("Wall area: " + wallArea + " square feet");                      //Correction made: 'wallArea' missing from print statement, 'wallArea' added

        // Calculate and output the amount of paint (in gallons) needed to paint the wall
        gallonsPaintNeeded = wallArea/squareFeetPerGallons;
        System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");             //Correction made: 'gallonspaintneeded' updated to 'gallonsPaintNeeded"

    }
}

标签: javaexceptiondo-while

解决方案


推荐阅读