java程式改寫

我想把這程式,利用迴圈讓使用者可自鍵盤輸入n組的正方形或長方形的長,寛資料,把這些四邊形的資料存在陣列中,最後找出這些四邊形中的最大面積;列印這些四邊形的長,寛及它的形狀(標示正方形或長方形)及最大面積值,請問該如何著手呢?

class Quad

{

private int length;

private int width;

private int area;

private int total;

public void Quad(int w){ this.width=w;}

public void Rectangle(int w,int l){ this.width=w;this.length=l;}

public int getlength() { return length ;}

public int getwidth() { return width ;}

public int getarea() {return area;}

public int gettotal() {return total;}

public int computerareaQ(){ return area=width*width;

}

public int computerareaR(){ return area=length*width;

}

public int computertotalR(){ return total=2*(length+width);

}

public int computertotalQ(){ return total=2*(width);

}

}

public class TestQuad

{ public static void main(String[] args)

Quad b = new Quad();

b.Quad(4);

System.out.println("邊長=4");

b.computerareaQ();

System.out.println("正方形面積="+b.getarea());

b.computertotalQ();

System.out.println("正方形邊長="+b.gettotal());

Quad a = new Quad();

a.Rectangle(4,5);

System.out.println("長=5,寬=4");

a.computerareaR();

System.out.println("長方形面積="+a.getarea());

a.computertotalR();

System.out.println("長方形邊長="+a.gettotal());

}

}

2 個解答

評分
  • Ashley
    Lv 7
    1 0 年前
    最佳解答

    import java.util.Scanner;

    class Quad {

    private int length;

    private int width;

    private int area;

    private int total;

    public void Quad(int w){ this.width = w; this.length = -1; }

    public void Rectangle(int w,int l){ this.width = w; this.length = l; }

    public int getlength() { return length ;}

    public int getwidth() { return width ;}

    public int getarea() {return area;}

    public int gettotal() {return total;}

    public int computerareaQ(){ return area = width * width; }

    public int computerareaR(){ return area = length * width; }

    public int computertotalR(){ return total = 2 * (length + width); }

    public int computertotalQ(){ return total = 4 * (width); }

    }

    public class TestQuad {

    public static void main(String[] args) {

    Scanner p = new Scanner(System.in);

    System.out.print("請輸入 n:"); int n = p.nextInt();

    int ans, w, l, i, max = -1, area;

    Quad a[] = new Quad[n];

    for( i = 0; i < n; i++ ){

    System.out.println("請輸入1.正方形 2.長方形(1/2):");

    ans = p.nextInt();

    if( ans == 1 ){

    a[i] = new Quad();

    System.out.println("請輸入正方形邊長"); w = p.nextInt();

    a[i].Quad(w);

    a[i].computerareaQ();

    a[i].computertotalQ();

    } else {

    a[i] = new Quad();

    System.out.println("請輸入長方形的長"); l = p.nextInt();

    System.out.println("請輸入長方形的寬"); w = p.nextInt();

    a[i].Rectangle(w,l);

    a[i].computerareaR();

    a[i].computertotalR();

    }

    area = a[i].getarea();

    if( max == -1 || area > a[max].getarea() )

    max = i;

    }

    System.out.println("輸入四邊形中最大面積為:");

    if( a[max].getlength() == -1 )

    System.out.println("正方形");

    else {

    System.out.println("長方形");

    System.out.println("長:" + a[max].getlength() );

    }

    System.out.println("寬:" + a[max].getwidth() );

    System.out.println("面積:" + a[max].getarea() );

    }

    }

    參考資料: 個人的程式設計經驗
  • 1 0 年前

    請參考我的做法

    import java.io.*;

    import java.util.*;

    public class Rect implements Comparable<Rect>{

    boolean isSquare = false;

    int l, w;

    public Rect(int x) {

    isSquare = true;

    l = x; w = x;

    }

    public Rect(int x, int y) {

    l = x; w = y;

    }

    public int area() {

    return l * w;

    }

    public int compareTo(Rect r) {

    return area() - r.area();

    }

    public String toString() {

    if (isSquare) return "正方形: 邊長 " + l;

    return "長方形: 長 " + l + ", 寬 " + w;

    }

    public static void main(String[] args) throws Exception {

    System.out.print("請輸入n: ");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n = Integer.parseInt(br.readLine());

    Rect[] ary = new Rect[n];

    for (int i = 0; i < n; i++) {

    System.out.print("請輸入正方形的邊長或長方形的長與寬: ");

    String s = br.readLine();

    String[] ary2 = s.split("[ ,]");

    if (ary2.length == 1) ary[i] = new Rect(Integer.parseInt(ary2[0]));

    else ary[i] = new Rect(Integer.parseInt(ary2[0]), Integer.parseInt(ary2[1]));

    }

    for (Rect r : ary) {

    System.out.println(r);

    }

    Arrays.sort(ary);

    System.out.println("最大面積為 " + ary[ary.length-1].area());

    }

    }

還有問題?馬上發問,尋求解答。