Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-找不到我的计算器方法的符号_Java_Class - Fatal编程技术网

Java-找不到我的计算器方法的符号

Java-找不到我的计算器方法的符号,java,class,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 s

我试图让我的程序与类一起工作,但输出错误显示:

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);  
    }
  }
}
其原因是未声明add()方法

删除类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); 

}
这同样适用于其他方法,如减法、乘法等

编辑:


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

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);  
}
}
}
试试这个

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);  
        }
      }

您确定必须为操作或方法创建类吗?您应该从类材料中查看类与方法这些是类,而不是方法。将您的加法、减法、除法和乘法类转换为普通方法,然后您可以调用它们,并且这些类的名称也与您尝试调用的方法不同。“multiply”这个名字除了在你试图调用它的地方之外,其他任何地方都不存在。我认为你想为操作而不是类创建方法。感谢这股神秘力量,有没有什么方法可以实现一个类,这样我就可以为它创建一个UML图?但是如果你需要它是面向对象的,你可以按照编辑中的指定尝试。非常感谢这是完美的。欢迎@user1521810谢谢。你知道我将如何在UML图中显示它吗?你可以在Eclipse中添加Eclipse“UML生成器插件”,然后从代码生成类图
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);  
        }
      }