英文:
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 = "GPU";
String object2 = "Processador";
String object3 = "SSD";
}
class GPUsNvidia
{
boolean setNvidiaGaming = false;
boolean setNvidiaProcess = false;
String [] placas = { "rtx", "tesla" }; //Token error here
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" };
}
}
Where is the error? I'm a novice in Java. The full error is
{
"resource": "/C:/Users/User/Documents/java_lojavirtual/Produtos.java",
"owner": "_generated_diagnostic_collection_name_#1",
"code": "1610612967",
"severity": 8,
"message": "Syntax error on token \";\", { expected after this token",
"source": "Java",
"startLineNumber": 13,
"startColumn": 42,
"endLineNumber": 13,
"endColumn": 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论