类和方法而调用(以四则运算为基础)
一.类的调用
1.代码
package 四则运算; public class a { public static void main(String[] args) { // TODO Auto-generated method stub Yunsuan a=new Yunsuan(); int i; for(i=0;i<100;i++) { a.aa(); } } }
package 四则运算; import java.util.Random; public class Yunsuan { int a; int b; int c; char s; public void aa() { Random random = new Random(); a=random.nextInt(100); b=random.nextInt(100); c=random.nextInt(4); if(c==0) { s='+'; } else if(c==1) { s='-'; } else if(c==2) { s='*'; } else { s='/'; } System.out.print(a); System.out.print(s); System.out.print(b); System.out.print("="); System.out.print(" "); } }
2.结果截图
二.方法的调用
1.代码
package 四则运算; import java.util.Random; public class aa { public static void main(String[] args) { // TODO Auto-generated method stub a(); } private static void a() { // TODO Auto-generated method stub int a,b; int i; for(i=0;i<10;i++) { Random random = new Random(); a=random.nextInt(100); b=random.nextInt(100); zz(a,b); } } private static void zz(int a, int b) { // TODO Auto-generated method stub char s; int c; Random random = new Random(); c=random.nextInt(4); if(c==0) { s='+'; } else if(c==1) { s='-'; } else if(c==2) { s='*'; } else { s='/'; } System.out.print(a); System.out.print(s); System.out.print(b); System.out.print("="); System.out.print(" "); } }
2.结果截图