Java public static void方法报Illegal start of expression错误排查
问题描述
编写的Java程序基于循环与自定义方法实现,支持两类功能:
- 面积计算:覆盖三角形、正方形、矩形三类图形的面积求解
- 基础数学运算:包含加法、减法、乘法、除法四种四则操作
已逐一检查所有函数的闭合括号,确认加法函数本身括号闭合正确,其余函数的闭合逻辑也无异常,但加法函数相关位置始终触发编译报错。
异常位置代码
main方法内的循环选择逻辑代码如下,代码编写到while循环的if-else判断块时中断:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); //WHILE LOOP FOR CHOICES while(true){//CHOICES LOOP System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); System.out.println("Input Choice of Process"); System.out.println("1 - Addition process"); System.out.println("2 - Subtraction process"); System.out.println("3 - Multiplication process"); System.out.println("4 - Division process"); System.out.println("5 - Compute process"); System.out.println("Your choice: "); int option = scan.nextInt(); System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); if(option == 1){ Add(); } else if(option == 2){ Sub(); } else if(option == 3){ Mul(); } else if(option == 4){ Div(); } else if(option == 5){ Com(); } else if((option>=6)&&(option<=100)){//INVALID System.out.println("Invalid Input, Please Input Choice Again."); } else{//输入其他数字时终止循环退出程序 break; }
报错截图如下:
后续编写的自定义方法完整代码如下:
public static void Add(){ System.out.println("ADDITION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); int add1=scan.nextInt(); System.out.println("2nd number: "); int add2=scan.nextInt(); int addtotal=add1+add2; if(addtotal>100){// 加法逻辑:和大于100输出High,小于等于100输出Low System.out.println("Total is "+addtotal+" High"); } else if(addtotal<100){ System.out.println("Total is "+addtotal+" Low"); } } public static void Sub(){//SUBTRACTION System.out.println("SUBTRACTION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); int sub1=scan.nextInt(); System.out.println("2nd number: "); int sub2=scan.nextInt(); int subtotal=sub1-sub2; if(subtotal<0){// 减法逻辑:差为负数输出Invalid,大于等于0输出Valid System.out.println("Invalid "); } else if(subtotal>0){ System.out.println("Total is "+subtotal+" Valid"); } } public static void Mul(){//MULTIPLICATION System.out.println("MULTIPLICATION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); double multi1=scan.nextDouble();//乘法支持小数输入 System.out.println("2nd number: "); double multi2=scan.nextDouble(); double multitotal=multi1*multi2; System.out.println("Total is "+multitotal); } public static void Div(){ System.out.println("DIVISION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); int div1=scan.nextInt(); System.out.println("2nd number: "); int div2=scan.nextInt(); int divtotal= div1 / div2; int divremainder= div1 % div2;//除法输出商和余数 System.out.println("Total is "+divtotal); System.out.println("Remainder is "+divremainder); } public static void Com(){// 选择5时进入面积计算逻辑,选择a/b/c对应不同图形,其他输入返回主菜单 System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); System.out.println("Input Choice of Process"); System.out.println("a - Rectangle"); System.out.println("b - Square"); System.out.println("c - Triangle"); System.out.println("Your choice: "); Scanner Keyboard = new Scanner(System.in); Scanner input = new Scanner(System.in); char choice = Keyboard.nextLine().charAt(0); System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); if (choice == 'A' || choice == 'a')//矩形面积 { System.out.println("Enter length of rectangle's base: "); double base = input.nextDouble(); System.out.println("Enter length of rectangle's height: "); double height = input.nextDouble(); double rArea = base * height; System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + "."); } else if (choice == 'B' || choice == 'b') //正方形面积 { System.out.println("Enter length of square's sides: "); double sSide = input.nextDouble(); double sArea = sSide * sSide; System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + "."); } else if (choice == 'C' || choice == 'c') //三角形面积 { System.out.println("Enter traingle's side length: "); double tSide = input.nextDouble(); double tArea = tSide * tSide * tSide; System.out.println("The area of a triangle with a side length of " + tSide + " is " + tArea + "."); } else //无效输入 { System.out.println("You've entered an invalid character."); } } }
报错原因
核心问题是括号不匹配、方法定义位置非法:
- 第一段代码中
main方法的while(true)循环没有编写对应的闭合右大括号,main方法本身也未闭合 Add/Sub/Mul/Div/Com这些自定义方法被直接写在了main方法内部,Java语法不允许在方法内部嵌套定义其他方法,因此编译器解析到public static void Add()时就会抛出语法错误,报错位置显示在Add方法附近,实际根因是前面的main方法没有正确闭合。- 代码还存在其他隐性问题:
scan是定义在main方法里的局部变量,其他自定义方法无法访问,后续会报变量找不到的错误- 三角形面积计算逻辑错误,现有代码计算的是边长的三次方,不符合三角形面积公式
- 除法逻辑未判断除数为0的边界情况,运行时会触发算术异常
- Com方法中重复创建了多个Scanner对象,复用同一个对象即可,否则还会出现输入流吞换行的问题
- 加法逻辑遗漏了两数之和等于100的分支判断,减法逻辑遗漏了差等于0的分支判断
修复方案
- 补全main方法内while循环、main方法自身的闭合括号,将所有自定义方法移到main方法外部、Main类的内部
- 将Scanner对象定义为类的静态成员变量,供所有方法统一访问
- 修正四则运算、面积计算的边界逻辑和公式错误
修复后的可运行代码结构参考:
import java.util.Scanner; public class Main{ // 定义全局静态Scanner对象,所有方法可直接访问 public static Scanner scan = new Scanner(System.in); public static void main(String[] args) { // 功能选择循环 while(true){ System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); System.out.println("Input Choice of Process"); System.out.println("1 - Addition process"); System.out.println("2 - Subtraction process"); System.out.println("3 - Multiplication process"); System.out.println("4 - Division process"); System.out.println("5 - Compute process"); System.out.println("Your choice: "); int option = scan.nextInt(); System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); if(option == 1){ Add(); } else if(option == 2){ Sub(); } else if(option == 3){ Mul(); } else if(option == 4){ Div(); } else if(option == 5){ Com(); } else if(option>=6 && option<=100){ System.out.println("Invalid Input, Please Input Choice Again."); } else { break; } } // 补全while循环和main方法的闭合括号 } // 所有自定义方法移到main方法外部 public static void Add(){ System.out.println("ADDITION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); int add1=scan.nextInt(); System.out.println("2nd number: "); int add2=scan.nextInt(); int addtotal=add1+add2; if(addtotal>100){ System.out.println("Total is "+addtotal+" High"); } else { System.out.println("Total is "+addtotal+" Low"); } } public static void Sub(){ System.out.println("SUBTRACTION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); int sub1=scan.nextInt(); System.out.println("2nd number: "); int sub2=scan.nextInt(); int subtotal=sub1-sub2; if(subtotal<0){ System.out.println("Invalid "); } else { System.out.println("Total is "+subtotal+" Valid"); } } public static void Mul(){ System.out.println("MULTIPLICATION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); double multi1=scan.nextDouble(); System.out.println("2nd number: "); double multi2=scan.nextDouble(); double multitotal=multi1*multi2; System.out.println("Total is "+multitotal); } public static void Div(){ System.out.println("DIVISION"); System.out.println("Enter two numbers: "); System.out.println("1st number: "); int div1=scan.nextInt(); System.out.println("2nd number: "); int div2=scan.nextInt(); // 增加除数为0的判断 if(div2 == 0){ System.out.println("Divisor cannot be 0"); return; } int divtotal= div1 / div2; int divremainder= div1 % div2; System.out.println("Total is "+divtotal); System.out.println("Remainder is "+divremainder); } public static void Com(){ System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); System.out.println("Input Choice of Process"); System.out.println("a - Rectangle"); System.out.println("b - Square"); System.out.println("c - Triangle"); System.out.println("Your choice: "); // 消耗上一次nextInt遗留的换行符,无需重复创建Scanner scan.nextLine(); char choice = scan.nextLine().charAt(0); System.out.println("∘₊✧─────────────────────────────────────✧₊∘"); if (choice == 'A' || choice == 'a'){ System.out.println("Enter length of rectangle's base: "); double base = scan.nextDouble(); System.out.println("Enter length of rectangle's height: "); double height = scan.nextDouble(); double rArea = base * height; System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + "."); } else if (choice == 'B' || choice == 'b') { System.out.println("Enter length of square's sides: "); double sSide = scan.nextDouble(); double sArea = sSide * sSide; System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + "."); } else if (choice == 'C' || choice == 'c') { // 修正三角形面积公式(以底乘高除以2为例) System.out.println("Enter triangle's base length: "); double tBase = scan.nextDouble(); System.out.println("Enter triangle's height: "); double tHeight = scan.nextDouble(); double tArea = tBase * tHeight / 2; System.out.println("The area of a triangle with a base of " + tBase + " and a height of " + tHeight + " is " + tArea + "."); } else { System.out.println("You've entered an invalid character."); } } }
内容的提问来源于stack exchange,提问作者mmmmmmmellie
相关产品推荐
相关产品推荐

