问题描述
在我的花括号上收到一个语法错误,但无论我关闭多少个它仍然会出现错误。想不通感谢您对此的任何建议,我不知所措。我尝试添加更多并删除更多。我计算了需要关闭的次数,但仍然出现错误。
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;
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's height
do {
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
while (!scnr.hasNextInt()) {
System.out.printf("\"%s\" is not a valid number.\n");
System.out.println("Please enter wall height in feet: ");
} while (wallHeight < 0) {
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
do {
System.out.println("Enter wall width (feet): ");
wallWidth = scnr.nextDouble(); // changed wallHeight to wallWidth
while (!scnr.hasNextDouble()) {
System.out.printf("\"%s\" is not a valid number.\n");
System.out.println("Please enter wall width in feet: ");
} while (wallWidth < 0) {
// Calculate and output wall area
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + wallArea + " square feet"); // added variable
// Calculate and output the amount of paint (in gallons) needed to paint the wall
gallonsPaintNeeded = wallArea/squareFeetPergallons;
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); // changed variable to correct case
}
}
解决方法
您尝试实现 do while 循环,但出现错误。
你必须让它看起来像那样
do {
//something
//something
}while(condition);
您可以在此处阅读有关此循环的更多信息:https://www.javatpoint.com/java-do-while-loop
,您似乎在 } while (wallWidth < 0) {
行中有一个额外的花括号。你有没有试过删除它?大括号没问题,但行尾的大括号可能会给您带来问题。
你的 do while 循环有问题。
第 19 行和第 29 行上的 do
永远不会关闭,并且最后缺少 while 子句。除此之外,您还缺少 4 个花括号。
public class Paint1 {
public static void main(String[] args) {
//...
do {
//...
while (!scnr.hasNextInt()) {
//..
}
while (wallHeight < 0) {
// ..
do {
// ..
while (!scnr.hasNextDouble()) {
}
while (wallWidth < 0) {
}
} while (true); // Should have a clause,or will be an infinite loop
}
} while (true); // Should have a clause,or will be an infinite loop
}
}