当前位置: 首页 > 知识库问答 >
问题:

函数public static void上的表达式开头非法

商振
2023-03-14

我的代码是关于循环和方法的,这是一个让用户计算面积或使用4个基本数学运算的程序。(三角形、正方形、矩形)及其选择的进程:加法、减法、乘法和除法。

我想我正确地关闭了那里的加法函数,我已经检查了关闭每个函数,它们似乎工作得很好,除了加法函数,因为这是我唯一的错误。

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{//- if user input other number, the program will break and stop from looping

                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){// In addition, if the sum is higher than 100, print the answer the word high. if equal and below 100, print 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){// In subtraction, if the difference is negative, print invalid. If 0 or above, print the difference and the word 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();//In multiplication, make it accepts decimal value

                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;//In division, if it has remainder, print the answer and the remainder.

            

                System.out.println("Total is "+divtotal);

                System.out.println("Remainder is "+divremainder);

            }

            public static void Com(){// If user choose 5, the user needs to choose again on a ,b or c. If other letter, print invalid and then go pack on choosing of process.





                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')//rectangle

                { 

                    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') //square

                { 

                    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') //traingle

                { 

                    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 //invalid

                { 

                System.out.println("You've entered an invalid character.");

            } 
}
}

共有1个答案

唐修能
2023-03-14

您的主方法没有右括号

 类似资料:
  • 问题内容: 我基本上是在完善,完成并尝试从Java初学者的参考书中编译测试代码。目的是创建一个猜谜游戏,其中目标位于3个连续的单元格中(我将位置保持在数组中),而用户则猜测该单元格的编号。逐个摧毁目标细胞。 我在这里检查了六则关于同一错误的帖子,但我无法弄清楚出了什么问题。 这是我的错误: 我的代码是: 问题答案: 方法只能声明局部变量。这就是为什么当您尝试将其声明为public时,编译器会报告错

  • 为什么我会得到:非法的表达式开始-公共静态int noOfLetters(String str){error?我如何修复它?

  • 本文向大家介绍JavaScript中的函数申明、函数表达式、箭头函数,包括了JavaScript中的函数申明、函数表达式、箭头函数的使用技巧和注意事项,需要的朋友参考一下 JavaScript中的函数可以通过几种方式创建,如下。 函数声明和表达式之间的差别是 JavaScript 解释器中存在一种变量声明被提升的机制,也就是说函数声明会被提升到作用域的最前面,即使写代码的时候是写在最后面,也还是会

  • 我不明白第77行不能从这个开始,但我用“public String showTrack()”开始了类似的内容。 这是我当前的代码:

  • 我正在从其他框架生成一个JTabbedFrame。

  • 问题内容: 我在注释标记的位置的以下代码中收到“表达式的非法开头”错误。我该如何纠正该错误? 问题答案: 您无法在Java中将方法彼此嵌套。移动外部方法。