我无法编译下面编写的代码,Java 总是抛出错误。你能帮我一下吗(P.S:我是Java新手,仍处于学习阶段)。如果我的代码有写错的地方,请帮我改正,以便我能学好。
抛出错误 (4): 1.加法不是抽象的,不会重写calci中的抽象方法Div()
减法不是抽象的,不会重写 calci 中的抽象方法 Div()
除法不是抽象的,不会重写 calci 中的抽象方法 Div()
乘法不是抽象的,不会覆盖 calci 中的抽象方法 Div()
import java.util.Scanner; interface calci { void add(); void sub(); void mul(); void div(); }
添加类
class addition implements calci
{
public void add()
{
System.out.println("Enter two numbers:");
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int n1= scn.nextInt();
int result = n+n1;
System.out.println("The Result of the two numbers is:"+result);
}
}
类别减法
class subtraction implements calci
{
public void sub()`enter code here`
{
System.out.println("Enter two numbers:");
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int n1= scn.nextInt();
int result = n-n1;
System.out.println("The Result of the two numbers is:"+result);
}
}
类乘法
class multiplication implements calci
{
public void mul()
{
System.out.println("Enter two numbers:");
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int n1= scn.nextInt();
int result = n*n1;
System.out.println("The Result of the two numbers is:"+result);
}
}
类(class)划分
class division implements calci
{
public void div()
{
System.out.println("Enter two numbers:");
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int n1= scn.nextInt();
int result = n/n1;
System.out.println("The Result of the two numbers is:"+result);
}
}
类计算器1
class calculator1
{
public static void main(String[] args)
{
int i =0;
addition a1 = new addition();
subtraction s1 = new subtraction();
multiplication m1 = new multiplication();
division d1 = new division();
System.out.println("Enter Your Name:");
Scanner scn = new Scanner(System.in);
String a = scn.next();
System.out.println("Good Morning!"+a);
System.out.println();
System.out.println("Please choose option from below");
System.out.println();
System.out.println("1.Addition 2.Subtraction 3.Multiplication 4.Division");
int option = scn.nextInt();
while (i!=0)
{
if (option==1)
{
a1.add();
}
else if (option == 2)
{
s1.sub();
}
else if (option == 3)
{
m1.mul();
}
else if (option == 4)
{
d1.div();
}
else
{
System.out.println("Please enter valid number");
}
}
System.out.println("Do you wish to continue");
int b = scn.nextInt();
if (b==0)
{
System.out.println("Thanks for using the calculator Program"+a);
System.out.println("Have a great day!");
}
}
}
最佳答案
当您实现一个接口(interface)时,您需要在实现类中实现该接口(interface)的所有方法。
在您的示例中,您应该实现 add()
、sub()
、mul()
和 div()
在任何实现接口(interface) calci
关于Java抛出错误 "<Class Name> is not abstract and does not override abstract method in the <Interface>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718142/