非法开始表达(方法) [英] Illegal start of expression (method)
问题描述
决定最近要学习一点Java,但我遇到了第一个障碍.这是我极其基本的代码:
decided to recently learn a little bit of java and I've been stumped at the first hurdle. Here is my extremely basic code:
import java.util.Scanner;
class helloWorld{
public static void main(String[] args){
Scanner user_input = new Scanner(System.in);
int a = 50;
String first_name;
String last_name;
public static int funcName(int a, int b) {
}
}
}
据我所知,没有错误.但是,在编译时我收到此错误:
As far as I can see, there are no errors. However, at compile time I receive this error:
Dominics-MacBook-Pro:helloworld dominicsore$ javac helloworld.java
helloworld.java:12: error: illegal start of expression
public static int funcName(int a, int b) {
^
helloworld.java:12: error: illegal start of expression
public static int funcName(int a, int b) {
^
helloworld.java:12: error: ';' expected
public static int funcName(int a, int b) {
^
helloworld.java:12: error: '.class' expected
public static int funcName(int a, int b) {
^
helloworld.java:12: error: ';' expected
public static int funcName(int a, int b) {
^
helloworld.java:12: error: ';' expected
public static int funcName(int a, int b) {
^
6 errors
我进行了搜索,所有常见的回答都是错别字和括号放错了位置,但据我所知并非如此.
I've searched and searched and all the usual responses are typos and misplaced brackets but as far as I can see this isn't the case.
不确定是否会有所不同,但是我使用的是vim编辑器,并且正在从终端进行编译.
Not sure if it will make any difference but I am on a mac, using the vim editor and I'm compiling from terminal.
任何建议都值得赞赏.
推荐答案
funcName
是在main方法中定义的,它必须在main方法之外:
funcName
is being defined from within the main method, it must be outside it:
import java.util.Scanner;
class helloWorld{
public static void main(String[] args){
Scanner user_input = new Scanner(System.in);
int a = 50;
String first_name;
String last_name;
}
public static int funcName(int a, int b) {
}
}
这篇关于非法开始表达(方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!