当前位置: 代码迷 >> Java Web开发 >> java小菜,类数组的问题,我的程序在传参数方面的问题,求帮忙!
  详细解决方案

java小菜,类数组的问题,我的程序在传参数方面的问题,求帮忙!

热度:286   发布时间:2011-09-04 20:40:57.0
java小菜,类数组的问题,我的程序在传参数方面的问题,求帮忙!
程序代码:
class Rectangle {
    int Length;
    int Width;
    int Area;
    int circumference;
    public void Rrectangle(int length, int width) {
        this.Length = length;
        this.Width = width;
    }
    public int getLength() {
        return Length;
    }
    public  int setLength(int length) {
        return this.Length = length;
    }
    public int getWidth() {
        return Width;
    }
    public void setWidth(int width) {
        this.Width = width;
    }
    public int getArea(int length,int width){
        return length*width;
    }
    public int getCircumference(int length,int width){
        return 2*(length+width);
    }
}
搜索更多相关主题的帖子: java  public  小菜  

----------------解决方案--------------------------------------------------------
程序代码:
import java.io.*;
import java.util.*;
public class Main{
    /**
     *
@param args
     
*/
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int maxArea = 0;
        int maxCircumference=0;
        int indexA = 0;
        int indexC;
        int area;
        int circumference;
        Scanner cin=new Scanner(System.in);
        Rectangle[]  a=new Rectangle[10];
        Rectangle[]  b=new Rectangle[10];
        System.out.print("请输入有几组数据");
        int n=cin.nextInt();
        for(int i=1;i<=n;i++)
        {
            int length=cin.nextInt();
            int width=cin.nextInt();
            a[i].Length=length;
            a[i].Width=width;
            a[i].Area=length*width;
            a[i].circumference=2*(length+width);
            if(a[i].Area>maxArea)
            {
                indexA=i;
            }
            if(a[i].circumference>maxCircumference)
            {
                indexC=i;
            }
        }
        System.out.print("面积最大的矩形的长为:"+a[indexA].Length+"宽为:"+a[indexA].Width+"最大面积为:"+maxArea);
    }
}

----------------解决方案--------------------------------------------------------
找出最大面积和周长的矩形!但是有异常!不知道哪里不对
----------------解决方案--------------------------------------------------------
呵呵
----------------解决方案--------------------------------------------------------
    问题很多:
1.
int Length;
    int Width;
    int Area;
    int circumference;
类参数应该设置为private类型,
   2.
         a[i].Length=length;
            a[i].Width=width;
            a[i].Area=length*width;
            a[i].circumference=2*(length+width);
这种访问不安全
3.   public void Rrectangle(int length, int width) {
        this.Length = length;
        this.Width = width;
    }

非构造函数
4.
        Rectangle[]  b=new Rectangle[10];
        System.out.print("请输入有几组数据");
        int n=cin.nextInt();
        for(int i=1;i<=n;i++)
n大于等于10的时候数组越界;且数组应从0开始

----------------解决方案--------------------------------------------------------
  相关解决方案