Java - 无法为我的计算器方法查找符号

时间:2016-06-22 12:46:51

标签: java class

我正在尝试让我的程序使用类,但输出错误显示:

Line: 32
cannot find symbol
symbol:   method add()
location: class maincalculator.MainCalculator

Line: 35
cannot find symbol
symbol:   method subtraction()
location: class maincalculator.MainCalculator

Line: 38
cannot find symbol
symbol:   method division()
location: class maincalculator.MainCalculator

Line: 41
cannot find symbol
symbol:   method multiply()
location: class maincalculator.MainCalculator

我想让这段代码工作,所以我可以完成我的任务,但我的任务是为不同的原始方法创建类。这是我当前的编译错误。

我的代码:

  package maincalculator;
//Imports the scanner that I will be using.
import java.util.Scanner;
/**
 *
 * @author alex
 */
public class MainCalculator {
     //Public static void for the class.
    public static void main(String[] args) {
        //Welcome print at the start of application.
        System.out.println("Welcome to the Calculator V1.O");
        //Blank line seperator.
        System.out.println("");
        System.out.println("What would you like to do today?");
        System.out.println("");
        //The options that will be shown to the program user in the console.
        //Option List for the program.
      System.out.println("1. Add");
      System.out.println("2. Subtract");
      System.out.println("3. Divide");
      System.out.println("4. Multiply");
      //Scans for the selected option.
      //Creates a new scanner.
      Scanner scan = new Scanner(System.in);
      //Checks if the next integer is selected.
      //Variables set to load the functions of each calculator.
      //Scans the integer choices.
      int choice = scan.nextInt();
        switch (choice) {
            case 1:
                add();
                break;
            case 2:
                subtraction();
                break;
            case 3:
                division();
                break;
            case 4:
                multiply();
                break;
            default:
                //Print this line if 1-4 are not selected.
                System.out.println("Option Unavailable");
                break;
        }
    }

 class add {
     public void addcode (){ 
     //Input a new scanner.
     Scanner input = new Scanner( System.in ); 
     //Collects the data from number1 and number2.
     int number1; 
     int number2; 
     int sum; 
     //Menu name
     System.out.println( "Add" );
     //Creates the message for the first number.
     System.out.print( "Enter your first number: " ); 
     //Creates the message for the second nmber.
     number1 = input.nextInt(); //Defines number1 
     System.out.print( "Enter your second number: " ); 
     number2 = input.nextInt(); //Defines number2 
     //Use addition symbol between the two numbers.
     sum = number1 + number2; 
     System.out.printf( "Sum equals %d\n", sum); 
    }
}

 class sub {
   public void subcode (){ 
   Scanner input = new Scanner( System.in ); 
     int number1; 
     int number2; 
     int sum; 
 System.out.println( "Subtraction" );
     System.out.print( "Enter your first number: " ); 
     number1 = input.nextInt(); //Defines number1 
     System.out.print( "Enter your second number: " ); 
     number2 = input.nextInt(); //Defines number2 

     sum = number1 - number2; 
     System.out.printf( "Sum equals %d\n", sum);  
    } 
}

class div {
   public void divcode (){ 
   Scanner input = new Scanner( System.in ); 
     int number1; 
     int number2; 
     int sum; 

     System.out.println( "Division" );
     System.out.print( "Enter your first number: " ); 
     number1 = input.nextInt(); //Defines number1 
     System.out.print( "Enter your second number: " ); 
     number2 = input.nextInt(); //Defines number2 

     sum = number1 / number2; 
     System.out.printf( "Sum equals %d\n", sum);  
     }
}

class multi {
    public void multicode (){ 
    //Multiplication
    //New scanner.
    Scanner input = new Scanner( System.in ); 
    //Int number from below.
     int number1; 
     int number2; 
     //The sum answer displayed.
     int sum; 

     System.out.println( "Multiply" );
     System.out.print( "Enter your first number: " ); 
     number1 = input.nextInt(); //Defines number1 
     System.out.print( "Enter your second number: " ); 
     number2 = input.nextInt(); //Defines number2 
     sum = number1 * number2; 
     System.out.printf( "Sum equals %d\n", sum);  
    }
  }
}

3 个答案:

答案 0 :(得分:1)

试试这个!!!!!!

package maincalculator;
    //Imports the scanner that I will be using.
    import java.util.Scanner;
    /**
     *
     * @author alex
     */
    public class MainCalculator {
         //Public static void for the class.
        public static void main(String[] args) {
            //Welcome print at the start of application.
            System.out.println("Welcome to the Calculator V1.O");
            //Blank line seperator.
            System.out.println("");
            System.out.println("What would you like to do today?");
            System.out.println("");
            //The options that will be shown to the program user in the console.
            //Option List for the program.
          System.out.println("1. Add");
          System.out.println("2. Subtract");
          System.out.println("3. Divide");
          System.out.println("4. Multiply");
          //Scans for the selected option.
          //Creates a new scanner.
          Scanner scan = new Scanner(System.in);
          //Checks if the next integer is selected.
          //Variables set to load the functions of each calculator.
          //Scans the integer choices.
          int choice = scan.nextInt();
            switch (choice) {
                case 1:
                    Add.addcode();
                    break;
                case 2:
                    Sub.subcode();
                    break;
                case 3:
                   Div.divcode();
                    break;
                case 4:
                    Multi.multicode();
                    break;
                default:
                    //Print this line if 1-4 are not selected.
                    System.out.println("Option Unavailable");
                    break;
            }
        }
    }
     class Add {
         public static void addcode (){ 
         //Input a new scanner.
         Scanner input = new Scanner( System.in ); 
         //Collects the data from number1 and number2.
         int number1; 
         int number2; 
         int sum; 
         //Menu name
         System.out.println( "Add" );
         //Creates the message for the first number.
         System.out.print( "Enter your first number: " ); 
         //Creates the message for the second nmber.
         number1 = input.nextInt(); //Defines number1 
         System.out.print( "Enter your second number: " ); 
         number2 = input.nextInt(); //Defines number2 
         //Use addition symbol between the two numbers.
         sum = number1 + number2; 
         System.out.printf( "Sum equals %d\n", sum); 
        }
    }

     class Sub {
       public static void subcode (){ 
       Scanner input = new Scanner( System.in ); 
         int number1; 
         int number2; 
         int sum; 
     System.out.println( "Subtraction" );
         System.out.print( "Enter your first number: " ); 
         number1 = input.nextInt(); //Defines number1 
         System.out.print( "Enter your second number: " ); 
         number2 = input.nextInt(); //Defines number2 

         sum = number1 - number2; 
         System.out.printf( "Sum equals %d\n", sum);  
        } 
    }

    class Div {
       public static void divcode (){ 
       Scanner input = new Scanner( System.in ); 
         int number1; 
         int number2; 
         int sum; 

         System.out.println( "Division" );
         System.out.print( "Enter your first number: " ); 
         number1 = input.nextInt(); //Defines number1 
         System.out.print( "Enter your second number: " ); 
         number2 = input.nextInt(); //Defines number2 

         sum = number1 / number2; 
         System.out.printf( "Sum equals %d\n", sum);  
         }
    }

    class Multi {
        public static   void multicode (){ 
        //Multiplication
        //New scanner.
        Scanner input = new Scanner( System.in ); 
        //Int number from below.
         int number1; 
         int number2; 
         //The sum answer displayed.
         int sum; 

         System.out.println( "Multiply" );
         System.out.print( "Enter your first number: " ); 
         number1 = input.nextInt(); //Defines number1 
         System.out.print( "Enter your second number: " ); 
         number2 = input.nextInt(); //Defines number2 
         sum = number1 * number2; 
         System.out.printf( "Sum equals %d\n", sum);  
        }
      }

答案 1 :(得分:0)

因为没有声明add()方法..

删除您的类添加,然后将此方法添加到类MainCalculator

public void add(){ 
     //Input a new scanner.
     Scanner input = new Scanner( System.in ); 
     //Collects the data from number1 and number2.
     int number1; 
     int number2; 
     int sum; 
     //Menu name
     System.out.println( "Add" );
     //Creates the message for the first number.
     System.out.print( "Enter your first number: " ); 
     //Creates the message for the second nmber.
     number1 = input.nextInt(); //Defines number1 
     System.out.print( "Enter your second number: " ); 
     number2 = input.nextInt(); //Defines number2 
     //Use addition symbol between the two numbers.
     sum = number1 + number2; 
     System.out.printf( "Sum equals %d\n", sum); 

}

同样适用于减法,乘法等其他方法

编辑:

public class Calculator
{
    private int solution;
    private int x;
    private int y;
    public calculator(int x,int y)
    {
        this.x=x;
        this.y=y;
    }

    public int add()
    {
       return x + y;
    }
    public int subtract()
    {
       return x - y;
    }
    public int multiply()
    {    
       return x * y;
    }
    public int divide()
    {
       solution = x / y;
       return solution;
    }

}

答案 2 :(得分:0)

在主类中创建每个实例,然后调用相应的方法:

package maincalculator;
//Imports the scanner that I will be using.
import java.util.Scanner;
/**
   *
* @author alex
*/
public class MainCalculator {
 //Public static void for the class.
public static void main(String[] args) {
    //Welcome print at the start of application.
    add addition = new add();
    sub subtraction = new sub();
    div division = new div();
    multi multiplication = new multi();
    System.out.println("Welcome to the Calculator V1.O");
    //Blank line seperator.
    System.out.println("");
    System.out.println("What would you like to do today?");
    System.out.println("");
    //The options that will be shown to the program user in the console.
    //Option List for the program.
  System.out.println("1. Add");
  System.out.println("2. Subtract");
  System.out.println("3. Divide");
  System.out.println("4. Multiply");
  //Scans for the selected option.
  //Creates a new scanner.
  Scanner scan = new Scanner(System.in);
  //Checks if the next integer is selected.
  //Variables set to load the functions of each calculator.
  //Scans the integer choices.
  int choice = scan.nextInt();
    switch (choice) {
        case 1:
            addition.addcode();
            break;
        case 2:
            subtraction.subcode();
            break;
        case 3:
            division.divcode();
            break;
        case 4:
            multiplication.multicode();
            break;
        default:
            //Print this line if 1-4 are not selected.
            System.out.println("Option Unavailable");
            break;
    }
}

 class add {
 public void addcode (){ 
 //Input a new scanner.
 Scanner input = new Scanner( System.in ); 
 //Collects the data from number1 and number2.
 int number1; 
 int number2; 
 int sum; 
 //Menu name
 System.out.println( "Add" );
 //Creates the message for the first number.
 System.out.print( "Enter your first number: " ); 
 //Creates the message for the second nmber.
 number1 = input.nextInt(); //Defines number1 
 System.out.print( "Enter your second number: " ); 
 number2 = input.nextInt(); //Defines number2 
 //Use addition symbol between the two numbers.
 sum = number1 + number2; 
 System.out.printf( "Sum equals %d\n", sum); 
}
}

class sub {
public void subcode (){ 
Scanner input = new Scanner( System.in ); 
 int number1; 
 int number2; 
 int sum; 
System.out.println( "Subtraction" );
 System.out.print( "Enter your first number: " ); 
 number1 = input.nextInt(); //Defines number1 
 System.out.print( "Enter your second number: " ); 
 number2 = input.nextInt(); //Defines number2 

 sum = number1 - number2; 
 System.out.printf( "Sum equals %d\n", sum);  
} 
}

class div {
public void divcode (){ 
Scanner input = new Scanner( System.in ); 
 int number1; 
 int number2; 
 int sum; 

 System.out.println( "Division" );
 System.out.print( "Enter your first number: " ); 
 number1 = input.nextInt(); //Defines number1 
 System.out.print( "Enter your second number: " ); 
 number2 = input.nextInt(); //Defines number2 

 sum = number1 / number2; 
 System.out.printf( "Sum equals %d\n", sum);  
 }
}

class multi {
public void multicode (){ 
//Multiplication
//New scanner.
Scanner input = new Scanner( System.in ); 
//Int number from below.
 int number1; 
 int number2; 
 //The sum answer displayed.
 int sum; 

 System.out.println( "Multiply" );
 System.out.print( "Enter your first number: " ); 
 number1 = input.nextInt(); //Defines number1 
 System.out.print( "Enter your second number: " ); 
 number2 = input.nextInt(); //Defines number2 
 sum = number1 * number2; 
 System.out.printf( "Sum equals %d\n", sum);  
}
}
}