【问题标题】:Syntax error on token 'else', { expected [closed]令牌'else'的语法错误,{预期[关闭]
【发布时间】:2014-09-25 20:17:47
【问题描述】:

我对 Java 有点陌生,我试着用正确的字符串编写这个简单的程序,让它打印出“密码输入”,当输入其他内容时,它会显示“密码错误”。但它给了我这个我不知道如何修复的错误,它说 令牌'else' 的语法错误,{ 预期的"

import java.util.Scanner;

public class Main {

    public static void main(String[] args){

        String code = ("1908");
        Scanner s = new Scanner(System.in);
        String input;

            System.out.println("Please enter password!");
        input = s.nextLine();

        if(input==code)
            System.out.println("Password entered!"); 
    } else { //<--- Syntax error on token 'else',{ expected
            System.out.println("Wrong password!");
            }

        }
    }   

【问题讨论】:

  • 你的缩进应该是一个大线索。

标签: java string if-statement input syntax


【解决方案1】:

在这一行if(input==code) 上,您忘记添加左括号,所以只需在末尾添加{

看起来像

if(input==code) {
    System.out.println("Password entered!"); 
} else { 
    System.out.println("Wrong password!");
}

为 cmets 编辑

System.out.println("Password entered!"); 从不打印的原因是因为您使用的是== 运算符。当== 运算符用于比较对象时,它会检查对象是否引用内存中的同一位置。基本上,它检查对象名称是否引用到相同的内存位置。不应该使用== 的示例如下:

// Create two Strings with the same text "abc"
String obj1 = new String("abc");
String obj2 = new String("abc");

// Compare the two Strings
if (obj1 == obj2) {
    // Will never reach here and print TRUE
    System.out.println("TRUE"); 
} else {
    // Will always print FALSE
    System.out.println("FALSE"); 
}

上面的代码总是打印FALSE,因为这两个字符串对象不在内存中的同一个地方——我知道这很混乱!要阐明何时使用==,请参阅下面的代码

// Create a new string object
String obj1 = new String("abc");
// Create another new string object but assign obj1 to it.
String obj2 = obj1; // obj1 and obj2 are now located in the same place in memory.

// Compare the two objects
if (obj1 == obj2) {
    // Will always print TRUE
    System.out.println("TRUE"); 
} else {
    // Will never print FALSE
    System.out.println("FALSE"); 
}

在上面的代码中,我们的两个对象位于内存中的同一位置,因此在比较每个对象的内存引用时,它们在某种意义上是相等的。

但很明显,您不想比较对象的内存引用,而是要比较每个对象包含的字符串值。所以您需要查看的是equals() 方法,我不会对此进行详细介绍,但它主要比较两个对象值而不是内存位置。看看下面的代码

// Create two Strings with the same text "abc"
String obj1 = new String("abc");
String obj2 = new String("abc");

// Compare the two String values using equals()
if (obj1.equals(obj2)) {
    // Will always print TRUE because the two string values
    // are equal/the same.
    System.out.println("TRUE"); 
} else {
    // Will never print FALSE
    System.out.println("FALSE"); 
}

因此,使用我们刚刚了解的 == 运算符和 equals() 方法,我们可以在您的代码中使用它,因此请尝试更改为

// Using equals because you want to compare the String values
// not the reference to memory location for each object
if(input.equals(code)) {
    // Should print true if input value is equal to code value.
    System.out.println("Password entered!"); 
} else { 
    System.out.println("Wrong password!");
}

如果您还有问题,请发表评论,希望对您有所帮助。

【讨论】:

  • 谢谢!虽然我在添加 { 后遇到了问题,但它现在不打印“输入密码!”了。另外,我将 } else { 更改为 } else if(!code.equals(input)) { 以确保它不检查特定字符串。
  • @FrederikLarsenBang 我已经编辑了我的答案,包括解释为什么不在您的场景中使用 == 运算符以及该怎么做。请参阅标记为为 cmets 编辑 的部分
【解决方案2】:

您没有使用{ 在 if() 中打开 'true' 子句,因此当遇到 } else 时,} 实际上会终止整个 if()

应该是以下之一:

if (...) {
   ...
} else {
   ...
}

if (...)
  ...
else {
  ...
}

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 2013-07-16
    • 2014-12-03
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多