2016-06-22 4 views
-2

のシンボルを検索することはできません私は私のプログラムは、クラスでの作業を取得しようとしていますが、出力エラーが表示さ:Javaは - 私の電卓方法

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

操作やメソッドのクラスを作成する必要がありますか?あなたのクラスの教材からクラスとメソッドを見直すべきです –

+1

これはメソッドではなくクラスです。 – tkausl

+1

あなたのadd、subtract、div、multipllyクラスをプレーンメソッドに変換してから、それらを呼び出すことができます – Sanjeev

答えて

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

ありがとう、これは完璧です。 – user1521810

+0

ようこそ@ user1521810 –

+0

非常にありがとうPSMは、これをUML図でどのように表示するのか知っていますか? – user1521810

0

そのため、(追加)メソッドが宣言されていません。..

追加するには、クラスを削除し、代わりにクラス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; 
    } 

} 
+0

このMysticForceをありがとう、私はそれのためのUML図を作成することができます私はクラスを実装する方法はありますか? – user1521810

+0

あなたはそれを行うことができます..しかし、クラスとメソッドの全体的な感覚はそこに失われます.. – MysticForce

+0

しかし、あなたがオブジェクト指向にする必要がある場合は、編集で指定されたように試すことができます – MysticForce

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