JAVA curly braces



在我的大括号上收到语法错误,但无论我关闭多少次,它仍然会出错。搞不清楚。谢谢你在这方面的建议,我不知所措。我已经尝试添加和删除更多。我已经计算了需要关闭的数量,但仍然出现了错误。

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
}
}

最新更新