运行会出错,拜托有时间的给看看!!!
创建一个含有10个元素的Alone6_2的数组ob,使其满足下列条件,并使其像下面提示进行输出:
条件:
ob[0].a=1,ob[0].b=2
ob[i+1].a=ob[i].a+ob[i].b;
ob[i+1].b=ob[i].a*ob[i].b;
结果:
ob[0].a=1,ob[0].b=2
ob[1].a=3,ob[1].b=2
ob[2].a=5,ob[2].b=6
......
代码:
public class Alone6_2
{
private int a,b;
public Alone6_2(){}
public void setAB(int a,int b)
{
this.a=a;
this.b=b;
}
public int getA()
{
return a;
}
public int getB()
{
return b;
}
public static void main(String[] args)
{
Alone6_2[] ob=new Alone6_2[10];
for(int i=0;i<ob.length;i++)
{
ob[i]=new Alone6_2();
ob[0].setAB(1,2);
for(int i=1;i<ob.length;i++)
{
ob[i+1].a=ob[i].a+ob[i].b;
ob[i+1].getA();
ob[i+1].b=ob[i].a*ob[i].b;
ob[i+1].getB();
System.out.println("ob["+i+"]"+".a"+"="+ob[i].a);
System.out.println("ob["+i+"]"+".b"+"="+ob[i].b);
}
}
}
创建一个含有10个元素的Alone6_2的数组ob,使其满足下列条件,并使其像下面提示进行输出:
条件:
ob[0].a=1,ob[0].b=2
ob[i+1].a=ob[i].a+ob[i].b;
ob[i+1].b=ob[i].a*ob[i].b;
结果:
ob[0].a=1,ob[0].b=2
ob[1].a=3,ob[1].b=2
ob[2].a=5,ob[2].b=6
......
代码:
public class Alone6_2
{
private int a,b;
public Alone6_2(){}
public void setAB(int a,int b)
{
this.a=a;
this.b=b;
}
public int getA()
{
return a;
}
public int getB()
{
return b;
}
public static void main(String[] args)
{
Alone6_2[] ob=new Alone6_2[10];
for(int i=0;i<ob.length;i++)
{
ob[i]=new Alone6_2();
ob[0].setAB(1,2);
for(int i=1;i<ob.length;i++)
{
ob[i+1].a=ob[i].a+ob[i].b;
ob[i+1].getA();
ob[i+1].b=ob[i].a*ob[i].b;
ob[i+1].getB();
System.out.println("ob["+i+"]"+".a"+"="+ob[i].a);
System.out.println("ob["+i+"]"+".b"+"="+ob[i].b);
}
}
}
解决方案 »
- 请教一个String数组遍历加分割的问题
- 有没有用过iText的?ITextRenderer这个类为什么找不到啊?
- eclipse中的什么插件带有像mfc中的拖拽形式,swt可以吗?在哪下?
- 谁能弄个例子说明一个事件监听是怎么用的?
- 新手,请各位帮忙看个ServerSocket的程序,谢谢
- 关于输入输出
- applet中,重载了paint(),按钮啊什么的就不显示了
- jbuilder2005怎么可以生产jar
- 向各位大侠请教一个用jaxp包生成xml对象的问题
- 【求助】List<Object>中 Object和Class的关系
- 碰到这样的一道面试题,谁能给点思路?
- 怎么用Java 表示二维的点(新手)
public class Alone6_2 {
private int a, b; public Alone6_2() {
} public void setAB(int a, int b) {
this.a = a;
this.b = b;
} public int getA() {
return a;
} public int getB() {
return b;
} public static void main(String[] args) {
Alone6_2[] ob = new Alone6_2[10];
ob[0] = new Alone6_2();
ob[0].setAB(1, 2);
System.out.println("ob[" + 0 + "]" + ".a" + "=" + ob[0].a);
for (int j = 0; j < ob.length - 1; j++) {
ob[j + 1] = new Alone6_2();
ob[j + 1].a = ob[j].a + ob[j].b;
ob[j + 1].getA();
ob[j + 1].b = ob[j].a * ob[j].b;
ob[j + 1].getB();
System.out.println("ob[" + (j + 1) + "]" + ".a" + "=" + ob[j + 1].a);
System.out.println("ob[" + (j + 1) + "]" + ".b" + "=" + ob[j + 1].b);
}
}
}
//console
ob[0].a=1
ob[1].a=3
ob[1].b=2
ob[2].a=5
ob[2].b=6
ob[3].a=11
ob[3].b=30
ob[4].a=41
ob[4].b=330
ob[5].a=371
ob[5].b=13530
ob[6].a=13901
ob[6].b=5019630
ob[7].a=5033531
ob[7].b=1058399894
ob[8].a=1063433425
ob[8].b=-1642014574//Int的最大值是:2147483647,此处乘法已经超出该最大值,窄转换使数据失真.
ob[9].a=-578581149
ob[9].b=117143346