为什么我的 while 循环在 Java 中抛出错误?

问题描述 投票:0回答:1

这里是初学者。我在编写一个 while 循环(在 c 部分) 时遇到了麻烦,它输出区间 [35, 175[ 中所有可被 5 和 7 整除的数字,彼此相邻,并用空格分隔。我在 while 循环行遇到各种错误,我必须根据作业使用它。

这是我的代码: `

public class Assignment2 {

    public static void main(String[] args) {

        // TODO: Implement your solution
        String firstText = "A noteworthy and suitable language.";
        String secondText = "The number of characters is not enough!";

        // section a)
        int numberOfTexts = 1;

        while (numberOfTexts <= 1) { 
            String result = firstText
                    .replace("a", "-a") .replace("A", "-A")
                    .replace("e", "-e") .replace("E", "-E")
                    .replace("i", "-i") .replace("I", "-I")
                    .replace("o", "-o") .replace("O", "-O")
                    .replace("u", "-u") .replace("U", "-U");
            System.out.println(result);
            numberOfTexts++;
        }

        // section b)
        String result = "";
        int textLength = firstText.length();

        for (int i = 0; i < textLength; i++) {
            char currentLetter = firstText.charAt(i);
            if ((int) currentLetter >= 75 && (int) currentLetter <= 90) { //uppercase
                result += currentLetter;
            } else if ((int) currentLetter >= 107 && (int) currentLetter <= 122) { //lowercase
                result += currentLetter;
            }
        }
        System.out.println(result);
        result = "";
    }

    // section c)
    int num = 35;

    while (num < 175) { //line 45
        boolean requirement = (num % 5 == 0) && (num % 7 == 0);
        if (requirement) {
            System.out.print(num + " ");
        }
        num++;
    }
}`

错误如下:

意外标记:45,预期“>”或“,”:45,预期标识符:45,预期标识符:45,意外标记:45

我尝试使用 int start, end, current;但它给出了同样的错误。我还尝试摆脱布尔要求并仅在 if 语句中使用其内容,这给出了相同的结果(如预期)。谷歌搜索也没有多大帮助。 while 循环声明仍然带有红色下划线。

java error-handling while-loop identifier unexpected-token
1个回答
0
投票

有问题的代码(带有错误)位于方法声明的外部。 具体来说,您的

main
已关闭 (
}
)。

您必须弄清楚您是否打算将其包含在

main
方法中,还是正在考虑添加新方法。

© www.soinside.com 2019 - 2024. All rights reserved.