java - 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"
}
}
解决方案
推荐阅读
- billboard.js - 如何在 billboardJS 中使用转换日期
- curl - 客户端证书可以在标头中发送吗?
- sql - 如何更新此选择查询以更新?- 到目前为止,它只是挂起
- jquery - 加载成功从未在引导表中触发
- python - 如何忽略 Pandas Python 中的标题行
- java - java - 如何使用java中的线程创建多个坠落对象?
- c# - 无法在 C Sharp 的声明中指定构造函数参数
- excel - 如果某些工作表包含自动筛选而其他工作表不包含,则循环并选择范围
- slickgrid - 如何在 angular-slickgrid 中使用力配合列?
- php - 如何在 Laravel 中将数据从一个表复制到另一个包含所有列但没有 id 的表中?