import java.util.Scanner;
public class Addition
{
public Addition (int x , int y)
{
int sum;
int one = x;
int two = y;
sum =x+y;
System.out.println("Addition of Number:%d" +sum);
}
}
public class Subtraction{
public Subtraction(int x ,int y){
int sub;
int one = x;
int two = y;
sub =x-y;
System.out.println("Subtraction of Number: %d" +sub);
}
}
public class Multiplication
{
public Multiplication(int x ,int y){
int mul;
int one = x;
int two = y;
mul =x*y;
System.out.println("Multiplication of Numbers:%d" +mul);
}
}
public class Division{
public Division(int x , int y){
int div;
int one = x;
int two = y;
div = x/y;
}
}
public class Calculator
{
public static void main(String args[]){
int one;
int two;
String choice;
System.out.println("Enter numbers to calculate:");
Scanner input = new Scanner(System.in);
int one =input.nextInt();
System.out.println("Your First Number =" +one);
int two=input.nextInt();
System.out.println("Your First Number =" +one);
System.out.println("Enter your choice");
String choice = input.nextLine();
switch(choice){
case Add:
Addition A =new Addition(int one ; int two);
break;
case Sub:
Subtraction s =new Subtraction(int one; int two);
break;
case Mul:
Multiplication m =new Multiplication(int one; int two);
break;
case Div:
Division A =new Division(int one; int two);
break;
default:
System.out.println("Wrong Choice");
break;
}
}
}
我在做错误的地方告诉我 错误:
Calculator.java:55: error: '.class' expected
Addition a =new Addition(int one ; int two);
^
Calculator.java:55: error: ';' expected
Addition a =new Addition(int one ; int two);
^
Calculator.java:58: error: '.class' expected
Subtraction s =new Subtraction(int one; int two);
^
Calculator.java:58: error: ';' expected
Subtraction s =new Subtraction(int one; int two);
^
Calculator.java:61: error: '.class' expected
Multiplication m =new Multiplication(int one; int two);
^
Calculator.java:61: error: ';' expected
Multiplication m =new Multiplication(int one; int two);
^
Calculator.java:64: error: '.class' expected
Division d =new Division(int one; int two);
^
Calculator.java:64: error: ';' expected
Division d =new Division(int one; int two);
^
答案 0 :(得分:1)
Addition A =new Addition(int one ; int two);
错误正确的是:
Addition A =new Addition(one , two);
这也适用于其他初始化。
专门阅读this 6和7,了解有关函数和函数调用的更多信息,这是简单的可视化教程。
答案 1 :(得分:1)
你已经定义了'one'和'two'的类型:
int one =input.nextInt();
int two=input.nextInt();
因此,在调用新对象时,无需在参数列表中重新定义它。所以只要你写下来就可以了。:
Addition A =new Addition(one,two);
或
Subtraction s =new Subtraction(one, two);
参数列表也以逗号分隔。我已经在上面的代码括号中修复了它。
答案 2 :(得分:1)
这里有多个错误 1.按照指定在主方法切换案例中创建加法,减法等类的对象时删除分号(;)。 即所有案件的以下内容
Addition A =new Addition(one,two);
2.你在main方法中创建了两个原始的两个。只保留一次。 将变量声明保留为int one; int two;字符串选择
将代码更改为
one =input.nextInt();//remove int in the beginning
two=input.nextInt();//remove int in the beginning
choice = input.nextLine();
3.您需要在开关案例中声明“添加”,“子”,“Div”,“Mul”。 交换机使用byte,short,char和int原始数据类型。它也适用于枚举类型(在枚举类型中讨论),String类和一些包含某些基本类型的特殊类:Character,Byte,Short和Integer(在Numbers和Strings中讨论)。
import java.util.Scanner;
class Addition
{
public Addition(int x , int y)
{
int sum;
int one = x;
int two = y;
sum =x+y;
System.out.println("Addition of Number: " +sum);
}
}
class Subtraction{
public Subtraction(int x ,int y){
int sub;
int one = x;
int two = y;
sub =x-y;
System.out.println("Subtraction of Number: " +sub);
}
}
class Multiplication
{
public Multiplication(int x ,int y){
int mul;
int one = x;
int two = y;
mul =x*y;
System.out.println("Multiplication of Numbers: " +mul);
}
}
class Division{
public Division(int x , int y){
int div;
int one = x;
int two = y;
div = x/y;
System.out.println("Division of Numbers: " +div);
}
}
public class Calculator
{
public static void main(String args[]){
int one;
int two;
String choice;
System.out.print("Enter Choice Add / Sub / Mul / Div");
Scanner input1 = new Scanner(System.in);
choice = input1.nextLine();
System.out.println("Enter numbers to calculate:");
one =input1.nextInt();
two=input1.nextInt();
input1.close();
System.out.println("Your First Number =" +one);
System.out.println("Your Second Number =" +two);
System.out.println("Your choice="+choice);
final String Add = "Add";
final String Sub = "Sub";
final String Mul = "Mul";
final String Div = "Div";
switch(choice){
case Add:
Addition A =new Addition(one ,two);
break;
case Sub:
Subtraction s =new Subtraction(one,two);
break;
case Mul:
Multiplication m =new Multiplication(one,two);
break;
case Div:
Division d =new Division(one,two);
break;
default:
System.out.println("Wrong Choice");
break;
}
}
}
答案 3 :(得分:0)
调用构造函数时必须删除该类型。 参数列表以逗号分隔。
Addition A =new Addition(one,two);