为什么我在 Java 中的 while 循环中遇到错误? [已关闭]

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

这里是初学者。我在编写一个 while 循环(在 c 部分) 时遇到了麻烦,该循环输出所有可被 5 和 7 整除的数字,这些数字在区间 [35, 175] 中彼此相邻,并用空格分隔。我在

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
投票

这部分代码位于方法或初始化程序中。

    int num = 35;

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

您只需将该代码放入

main
方法中 - 如下面的代码所示。

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++;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.