public class Lianxi1 { // 属性 成员变量 public String name; public String sex; public int age; public String birthday; public String showName(){ System.out.println(name); return name; }
public class Data { public static void main(String[] args) { Lianxi1 lianxi=new Lianxi1(); lianxi.name="list"; lianxi.showName();//字母大小写 } }
计算加减乘除
方式一:
public class Jisuanqi { public int add(int a,int b){ return (a+b); } public int jian(int a,int b){ return (a-b); } public int cheng(int a,int b){ return (a*b); } public float chu(float a,float b){ return (a/b); } }
public class Data { public static void main(String[] args) { Jisuanqi j=new Jisuanqi(); int c=j.add(3,5); System.out.println(c); Jisuanqi h=new Jisuanqi(); int d=h.jian(3,5); System.out.println(d); Jisuanqi g=new Jisuanqi(); int e=g.cheng(3,5); System.out.println(e); Jisuanqi k=new Jisuanqi(); float f=k.chu(5,3); System.out.println(f); } } }
方式二:
public class Jisuan {
double c=0;
public double jisuan(double a,double b,String f){
if(f.equals("+")){
c=a+b;
}else if(f.equals("-")){
c=a-b;
}else if(f.equals("*")){
c=a*b;
}else if(f.equals("/")){
c=a/b;
}
return(c);
}
}
public class Data {
public static void main(String[] args) {
Jisuan j=new Jisuan();
double c=j.jisuan(5, 3, "+");
System.out.println(c);
}
}
时间: 2024-10-13 02:14:30