簡體   English   中英

JAVA 花括號

[英]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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM