英文:
expected '{' when braces are matching correctly
问题
以下是您要求的翻译内容:
嗨,我是(**非常)不熟悉Java,我有C++的背景,
我遇到了这个错误:
Array-Lists-In-java.java:3: 错误: 期望'{'
public class Array-Lists-In-java{
^
这是我的代码:
import java.util.ArrayList;
public class Array-Lists-In-java {
public static void main(String[] args)
{
ArrayList<String> grocery = {"Onions","Carrots","Olives", "olive oil"};
printArray(grocery);
grocery.add("Tomatoes");
printArray(grocery);
}
public static void printArray(ArrayList<String> list)
{
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
}
}
我做错了什么?请纠正我错误的地方,这将非常有帮助
谢谢
英文:
Hi I am (very) new to java and I come from a c++ background
and I am getting this error:
Array-Lists-In-java.java:3: error: '{' expected
public class Array-Lists-In-java{
^
and here is my code:
import java.util.ArrayList;
public class Array-Lists-In-java {
public static void main(String[] args)
{
ArrayList<String> grocery = {"Onions","Carrots","Olives", "olive oil"};
printArray(grocery);
grocery.add("Tomatoes");
printArray(grocery);
}
public static void printArray(ArrayList<String> list)
{
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
}
}
what am I doing wrong??pls correct me where i am going wrong it would be really helpful
thanks
答案1
得分: 0
嘿,python_is_good_cpp_is_best!我认为你可能缺少或者多了一个花括号,记住每个花括号都应该有一个配对的伙伴!尝试一下数一下你有多少个开放的花括号,以及你有多少个闭合的花括号。如果它们数量相同的话,记得使用正确的命名约定。编程语言不支持名字中带有“-”,因为它们会认为你想要做减法运算。你的类名中有一个“-”,尝试用下划线“_”来替代“-”,应该会正常工作!谢谢你!
英文:
Hey python_is_good_cpp_is_best! I think you are missing or have an extra flower bracket somewhere, remember each flower bracket should have a buddy! Try counting how many open flower brackets you have, and how many closed flower brackets you have. if they are the same amount, remember to use proper naming convention, programming languages dont support names having '-' cause they think you want to do subtraction, you have a '-' in your class name, try replacing '-' with underscores '_' it should work properly! thankyou!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论