“`java Java “message”: “语法错误,此处应为“;”之后需要“{”标记” “`

huangapple go评论87阅读模式
英文:

Java "message": "Syntax error on token \";\", { expected after this token"

问题

public class Produtos 
{
    String object1 = "GPU";
    String object2 = "Processador";
    String object3 = "SSD";
}

class GPUsNvidia 
{
    boolean setNvidiaGaming = false;
    boolean setNvidiaProcess = false;

    String [] placas = { "rtx", "tesla" }; // 在这里有一个令牌错误
    
    for (int i = 0; i < placas.length; i++) 
    {
        if (placas[i].equals("rtx")) 
        { 
            setNvidiaGaming = true; 
        }
        if (placas[i].equals("tesla")) 
        { 
            setNvidiaProcess = true; 
        }
    }

    if (setNvidiaGaming == true) 
    {
        final String [] subObject1 = { "2060", "2070", "2080" };
    }
}

注意:上述的代码中有一个错误。循环结构不应该直接位于类的定义中,需要放在方法内部。

英文:
public class Produtos 
{
	String object1 = &quot;GPU&quot;;
    String object2 = &quot;Processador&quot;;
    String object3 = &quot;SSD&quot;;
}

class GPUsNvidia 
{
    boolean setNvidiaGaming = false;
    boolean setNvidiaProcess = false;

    String [] placas = { &quot;rtx&quot;, &quot;tesla&quot; }; //Token error here

    for (int i = 0; i &lt; placas.length; i++) 
    {
        if (placas[i].equals(&quot;rtx&quot;)) 
        { 
            setNvidiaGaming = true; 
        }
        if (placas[i].equals(&quot;tesla&quot;)) 
        { 
            setNvidiaProcess= true; 
        }
    }

    if (setNvidiaGaming == true) 
    {
        final String [] subObject1 = { &quot;2060&quot;, &quot;2070&quot;, &quot;2080&quot; };
    }
}

Where is the error? I'm a novice in Java. The full error is

{
	&quot;resource&quot;: &quot;/C:/Users/User/Documents/java_lojavirtual/Produtos.java&quot;,
	&quot;owner&quot;: &quot;_generated_diagnostic_collection_name_#1&quot;,
	&quot;code&quot;: &quot;1610612967&quot;,
	&quot;severity&quot;: 8,
	&quot;message&quot;: &quot;Syntax error on token \&quot;;\&quot;, { expected after this token&quot;,
	&quot;source&quot;: &quot;Java&quot;,
	&quot;startLineNumber&quot;: 13,
	&quot;startColumn&quot;: 42,
	&quot;endLineNumber&quot;: 13,
	&quot;endColumn&quot;: 43
}

答案1

得分: 3

你不能简单地让代码块漂浮在类内部。它们需要被封装在方法、构造函数或初始化器中。

英文:

You can't just have blocks of code floating around inside classes. They need to be enclosed in a method, constructor or initializer.

huangapple
  • 本文由 发表于 2020年4月5日 03:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61033665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定