Java加减乘除

相关代码

package q;

import java.util.Scanner;

public class text
{
public static void main (String[] args){

Cal cal= new Cal(8,4);

System.out.println("8+4="+cal.add() );

System.out.println("8-4="+cal.sub() );

System.out.println("8*4="  +cal.mul() );

System.out.println("8/4="  +cal.div() );

}
}
class Cal{

int x,y;

Cal(int a,int b)//构造函数
{
x=a;

y=b;
}

int add(){return x+y;}

int sub() {return x-y;}

int mul() {return x*y;}

int div() {return x/y;}

}

可能出现的错误:

若出现No enclosing instance of type text is accessible. Must qualify the allocation with an enclosing instance of type text (e.g. x.new A() where x is an instance of text).错误,
错误原因:
为class类放在static中造成,
解决方法:
将class类取出,放在static即可改正错误

发布了38 篇原创文章 · 获赞 0 · 访问量 1128

猜你喜欢

转载自blog.csdn.net/weixin_45489155/article/details/104833166