【发布时间】:2019-07-01 14:10:57
【问题描述】:
使用 javac 编译这个简单的代码,它给了我以下错误
Test3.java:38: 错误:表达式的非法开始
公共静态 int minFun (int a, int b) {
我尝试在 main 之外声明变量(即 public static int a、b、c),但没有任何改变。
这让我感到困惑,因为我关注 this tutorial 并使用了一个非常相似的示例代码。
提前感谢您的帮助。
// Program to output the minimum of two integer numbers
import java.io.*;
public class Test3 {
public static void main (String args[]) {
int a, b, c;
String rA, rB;
InputStreamReader input = new InputStreamReader (System.in);
BufferedReader keyboard = new BufferedReader (input);
System.out.println ("Please, enter two integer numbers.");
try {
rA = keyboard.readLine ();
a = Integer.parseInt (rA);
rB = keyboard.readLine ();
b = Integer.parseInt (rB);
}
catch (IOException e) {
System.err.println ("Not a proper integer number.");
}
catch (NumberFormatException e) {
System.err.println ("Not a proper integer number.");
}
c = minFun (a, b);
if (a != b) {
System.out.println ("The smaller number is " + c);
}
else {
System.out.println ("The two numbers are equals.");
}
public static int minFun (int a, int b) {
int min;
if (a < b) {
min = a;
}
else {
min = b;
}
return min;
}
}
【问题讨论】:
-
main 的最后一个 else 缺少右大括号。
-
方法签名之前的最后一个大括号是关闭
else块而不是main方法的那个。在它之后添加一个... -
Ffs 就是这么简单,抱歉浪费了大家的时间
-
并跟进这两个 cmets:出现问题的原因是,如果没有那个大括号,
else之后的未缩进大括号只会关闭那个else子句——但是让您保持在main函数内。这意味着public static int minFunction是main函数中的一行,这是不允许的。您不能在 Java 中的另一个函数中定义一个函数。
标签: java