“<标识符>”预计” Java中的编译错误

发布于 2024-10-30 03:37:46 字数 1749 浏览 1 评论 0原文

我在这一行收到“预期”错误......

  private static String getReducedISBN(char 'x') {

这段代码......

public class CheckISBN7 {

//private static String originalISBN;  // class variable

public static void main(String[] args) {
    // Prompt the user to enter an ISBN
    SimpleIO.prompt("Enter ISBN: ");
    String originalISBN = SimpleIO.readLine();

    // Get the ISBN number without the dashes
    String reducedISBN = getReducedISBN('-');

    // Get the computed check digit
    int computedCheckDigit = getCheckDigit(reducedISBN);

    // Display check digit entered by the user
    System.out.println("Check digit entered: " + originalISBN.charAt(12));

    // Display computed check digit
    System.out.println("Check digit computed: " + computedCheckDigit);
}

private static String getReducedISBN(char 'x') {
    SimpleIO.prompt("Enter ISBN: ");
    String originalISBN = SimpleIO.readLine();
    int dashPos1 = originalISBN.indexOf("x");
    int dashPos2 = originalISBN.indexOf("x", dashPos1 + 1);
    String reducedISBN = originalISBN.substring(0, dashPos1) +
            originalISBN.substring(dashPos1 + 1, dashPos2) +
            originalISBN.substring(dashPos2 + 1, 11);
    return reducedISBN;
}

private static int getCheckDigit(String reducedISBNParameter) {
    int total = 0;
    final String digits = "0123456789X";
    for(int i = 0, j = 10; i <= 8; i++, j++) {
        total += j *
                (Integer.parseInt(reducedISBNParameter.substring(i, i + 1)));
    }
    int checkDigit = 10 - ((total - 1) % 11);
    int computedCheckDigit = digits.charAt(checkDigit);
    return computedCheckDigit;
    }
}

无法真正找出问题,任何帮助将不胜感激。

I get "<identifier> expected" error on this line....

  private static String getReducedISBN(char 'x') {

...of this code....

public class CheckISBN7 {

//private static String originalISBN;  // class variable

public static void main(String[] args) {
    // Prompt the user to enter an ISBN
    SimpleIO.prompt("Enter ISBN: ");
    String originalISBN = SimpleIO.readLine();

    // Get the ISBN number without the dashes
    String reducedISBN = getReducedISBN('-');

    // Get the computed check digit
    int computedCheckDigit = getCheckDigit(reducedISBN);

    // Display check digit entered by the user
    System.out.println("Check digit entered: " + originalISBN.charAt(12));

    // Display computed check digit
    System.out.println("Check digit computed: " + computedCheckDigit);
}

private static String getReducedISBN(char 'x') {
    SimpleIO.prompt("Enter ISBN: ");
    String originalISBN = SimpleIO.readLine();
    int dashPos1 = originalISBN.indexOf("x");
    int dashPos2 = originalISBN.indexOf("x", dashPos1 + 1);
    String reducedISBN = originalISBN.substring(0, dashPos1) +
            originalISBN.substring(dashPos1 + 1, dashPos2) +
            originalISBN.substring(dashPos2 + 1, 11);
    return reducedISBN;
}

private static int getCheckDigit(String reducedISBNParameter) {
    int total = 0;
    final String digits = "0123456789X";
    for(int i = 0, j = 10; i <= 8; i++, j++) {
        total += j *
                (Integer.parseInt(reducedISBNParameter.substring(i, i + 1)));
    }
    int checkDigit = 10 - ((total - 1) % 11);
    int computedCheckDigit = digits.charAt(checkDigit);
    return computedCheckDigit;
    }
}

Can't really figure out the problem , any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-11-06 03:37:46

'x' 不是一个标识符(变量或其他),它是一个文字字符。同样,“x”是一个文字字符串。将 char 'x' 替换为 Character x,将 "x" 替换为 x.toString() 即可得到您想要的内容想。

'x' is not an indentifier (variable or whatever), it is a literal character. Likewise "x" is a literal string. Replace char 'x' with Character x and "x" with x.toString() to get what you want.

流殇 2024-11-06 03:37:46

您尝试在方法签名中传递 char 值 'x',这不是有效的语法:

private static String getReducedISBN(char 'x') {

您是否打算使用 x 作为变量名?

private static String getReducedISBN(char x) {

与这里一样,因为我假设您正在尝试查找作为分隔符而不是字符串 "x" 传递的任何内容的索引:

int dashPos1 = originalISBN.indexOf(x);
int dashPos2 = originalISBN.indexOf(x, dashPos1 + 1);

You're trying to pass the char value 'x' in your method signature, which isn't valid syntax:

private static String getReducedISBN(char 'x') {

Did you mean to use x as a variable name?

private static String getReducedISBN(char x) {

As well as here, since I assume you're trying to find the index of whatever you pass as the separator character instead of the string "x":

int dashPos1 = originalISBN.indexOf(x);
int dashPos2 = originalISBN.indexOf(x, dashPos1 + 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文