问题描述
最近决定学习一点 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.
不确定它是否会有所不同,但我在 Mac 上,使用 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 方法内部定义的,它必须在它之外:
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) {
}
}
这篇关于表达式的非法开始(方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!