使用arralist和looop

时间:2016-04-06 22:57:28

标签: java

我被要求从用户输入的一组有效矩形中输出最小和最大矩形的索引。

我进行了过滤以将有效矩形存储到arraylist' recB'。但是,当我从有效矩形中找出最大和最小的矩形时,它似乎永远不会正确地输出最小和最大的矩形。

矩形由用户的索引输入标记,从1-4开始。我宣布两个' int' named' smallestIndex = 0'和' largestIndex = 0'将索引放入谁,然后输出它们。但它总是打印像#34;最小:矩形0,最大:xxx"或者相反。

所以我认为" smallestIndex"或" largestIndex"可能没有对齐正确的索引,但无法弄明白。 我附上了确定无效代码并存储有效代码的代码如下:

//Determing and display the invalid rectangles
//and store the valid rectangles to arraylist 'recB' 
//Determining invalid ones
System.out.println("Invalid rectangles which are out of screen range: ");
for(int i=0; i<rec.length; i++){
  if(rec[i].getX()+rec[i].getWidth()>1000||(rec[i].getY()+rec[i].getHeight())>800){ 
    System.out.printf("Rectangle %d",rec[i].getIndex());
    rec[i].outputRectangle();
  }
//Storing the valid ones
  else{
    recB.add(rec[i]);
  }
}

//5.Determing and diplay index of the smallest and largest valid rectangle
//Create variable to store index of smallest and largest rectangle
int smallestIndex=0;
int largestIndex=0;
//Smallest rectangle

smallest = recB.get(0).getWidth()*recB.get(0).getHeight();
for(Rectangle rr:recB){
  if(rr.getWidth()-rr.getX()*rr.getHeight()<smallest){
    smallestIndex=rr.getIndex();
  }

}


//Largest rectangle
largest = recB.get(0).getWidth()*recB.get(0).getHeight();;
for(int i=0; i<recB.size(); i++){
  if(largest< recB.get(0).getWidth()*recB.get(0).getHeight()){
    largestIndex=recB.get(i).getIndex();
  }
}

//Output smallest and largest rectangle
System.out.printf("The Smallest: Rectangle %d\n",smallestIndex);
System.out.printf("The Largest: Rectangle %d\n",largestIndex);  

} }

只是提醒我把Rectangle类,修改后的RecTester和下面的输出。

Rectangle class:
import java.util.Scanner;
public class Rectangle {
  //Create instance variables//
  private int index;
  private int x;
  private int y;
  private int width;
  private int height;

  //Create Scanner
  Scanner input = new Scanner(System.in);

  //Declare default Constructor//
  public Rectangle(){

  }

  //Declare alternative Constructor//
  public Rectangle(int index, int x, int y, int width, int height){
    this.index=index;
    this.x=x;
    this.y=y;
    this.width=width;
    this.height=height;
  }

  //Declare methods//
  //1 Create setXY to set x,y for rectangle
  public void setXY(int x, int y){
    this.x=x;
    this.y=y;    
  }
  //2 Create getX to get value of X from rectangle
  public int getX(){
    return this.x;
  }
  //3 Create getY to get value of Y from rectangle
  public int getY(){
    return this.y;
  }

  //4 Create setWidth to set Width for rectangle
  public void setWidth(int width){
    this.width=width;
  }

  //5 Create getWidth to get value of Width from rectangle
  public int getWidth(){
    return this.width;
  }

  //6 Create setWidth to set Width for rectangle
  public void setHeight(int height){
    this.height=height;
  }

  //7 Create getHeight to get value of Height from rectangle
  public int getHeight(){
    return this.height;
  }

  //8 Create inputRectangle() for user to input data from keyboard.
  public void inputRectangle(){
    //Input valid value of X
    System.out.print("Index: ");
    index=input.nextInt();
    System.out.print("x: ");
    this.x=input.nextInt();
    if(x<1||x>1000){
      System.out.print("The valid x is 1-1000, please input again.");
      this.x=input.nextInt();
    }

    //Input valid value of Y
    System.out.print("y: ");
    this.y=input.nextInt();
    if(y<1||y>800){
      System.out.println("The valid y is 1-800, please input again.");
      this.y=input.nextInt();
    }

    //Input valid value of Width
    System.out.print("width: ");
    this.width=input.nextInt();
    if(width<1||width>1000){
      System.out.println("The valid width is 1-1000, please input again.");
      this.width=input.nextInt();
    }

    //Input valid value of Height
    System.out.println("height: ");
    this.height=input.nextInt();
    if(height<1||height>800){
      System.out.println("The valid Height is 1-800, please input again.");
      this.height=input.nextInt();
    }  
  }

   //9 Create outputRectangle() to print out the info. of Rectangle.

  public void outputRectangle(){
    System.out.printf("(x,y)= (%d,%d)\n",x,y);
    System.out.printf("width= %d\n", width);
    System.out.printf("height= %d\n", height); 
  }

  //10 Create getIndex
  public int getIndex(){
    return index;
  }
}

修订后的RecTester:

import java.util.ArrayList;

public class RecTester_v2 {


  public static void main(String[] args) {
    //1. Create an array storing 4 rectangle
    Rectangle[] rec = new Rectangle[4];
    //Create an ArrayList to store valid rectangle
    ArrayList<Rectangle> recB = new ArrayList<Rectangle>();




    //2. Ouput prompt and user inputs the rectangle from keyboard
    for(int i=0; i<rec.length; i++){
      rec[i]=new Rectangle();
      System.out.printf("Rectangle %d\n",i+1);
      rec[i].inputRectangle();
    }
    //3. Output the information of each Rectangle ande diplay index of invalid rectangle
    //Output the information of each Rectangle
    for(int i=0; i<rec.length; i++){
      System.out.printf(" Rectangle %d\n",i+1);
      rec[i].outputRectangle();
    }

    //Determing and display the invalid rectangles
    //and store the valid rectangles to arraylist 'recB' 
    System.out.println("Invalid rectangles which are out of screen range: ");
    for(int i=0; i<rec.length; i++){
      if(rec[i].getX()+rec[i].getWidth()>1000||(rec[i].getY()+rec[i].getHeight())>800){ 
        System.out.printf("Rectangle %d\n",rec[i].getIndex());
        rec[i].outputRectangle();
      }
      else{
        recB.add(rec[i]);
      }
    }

    System.out.println("Size of recB is: "+recB.size()); //Test recB's size

    System.out.println(" Content of recB(Valid Rectangles)");//Test content of recB
    for(Rectangle rB: recB){
      System.out.printf("Rectangle %d\n",rB.getIndex());
      rB.outputRectangle();
    }


    //4.Determing and display average area of valid rectangles
    //Create variable to compute average area
    double average=0;
    int sum=0;
    int count=0;
    for(Rectangle r: rec){
      if(r.getX()+r.getWidth()<1000&&(r.getY()+r.getHeight())<800){
      sum=sum+(r.getWidth())*(r.getHeight());      
      count++;
      }
    }
    average=(double)sum/count;
    System.out.println("sum is: "+sum);// To examine value of sum
    System.out.printf("The average area of qualified rectangle is %.2f\n",average);

    //5.Determing and diplay index of the smallest and largest valid rectangle
    //Create variable to store index of smallest and largest rectangle
    int smallest=0;
    int smallestIndex=0;
    int largest=0;
    int largestIndex=0;
    //Smallest rectangle

    smallest = recB.get(0).getWidth()*recB.get(0).getHeight();
//    for(Rectangle rr:recB){
//      if(rr.getWidth()*rr.getHeight()<smallest){
////        smallest=(rr.getWidth()-rr.getX())*(rr.getHeight()-rr.getY());
//        smallestIndex=rr.getIndex();
//      }    
//    }
// 
    for(int i=0;i<recB.size();i++){
      if(smallest> recB.get(i).getWidth()*recB.get(i).getHeight()){
        smallestIndex=recB.get(i).getIndex();
      }  
    }
    //Largest rectangle
    largest = recB.get(0).getWidth()*recB.get(0).getHeight();
    for(int i=0; i<recB.size(); i++){
      if(largest< recB.get(i).getWidth()*recB.get(i).getHeight()){
//        largest = (recB.get(0).getWidth()-recB.get(0).getX())*(recB.get(0).getHeight()-recB.get(0).getY())
        largestIndex=recB.get(i).getIndex();
      }
    }

    //Output smallest and largest rectangle
    System.out.printf("The Smallest: Rectangle %d\n",smallestIndex);
    System.out.printf("The Largest: Rectangle %d\n",largestIndex);  
  }

}

输入和输出

 Rectangle 1
(x,y)= (1,1)
width= 5
height= 5

 Rectangle 2
(x,y)= (999,799)
width= 2
height= 2

 Rectangle 3
(x,y)= (1,1)
width= 20
height= 20

 Rectangle 4
(x,y)= (1,1)
width= 10
height= 10

Invalid rectangles which are out of screen range: 
Rectangle 2
(x,y)= (999,799)
width= 2
height= 2

Size of recB is: 3
 Content of recB
Rectangle 1(x,y)= (1,1)
width= 5
height= 5

Rectangle 3(x,y)= (1,1)
width= 20
height= 20

Rectangle 4(x,y)= (1,1)
width= 10
height= 10

sum is: 525
The average area of qualified rectangle is 175.00
The Smallest: Rectangle 0
The Largest: Rectangle 4
> 

1 个答案:

答案 0 :(得分:0)

首先,我会重新阅读您的代码,并确保您使用相同的样式。例如,您使用增强的for循环来计算最小的矩形,并使用标准for循环来计算最大的矩形。

其次,我会尝试以下代码

smallest = recB.get(0).getWidth() * recB.get(0).getHeight();
for (Rectangle rr : recB) {
  if (rr.getWidth() * rr.getHeight() < smallest) {
    smallestIndex = rr.getIndex();
  }
}

//Largest rectangle
largest = recB.get(0).getWidth() * recB.get(0).getHeight();
for (int i = 0; i < recB.size(); i++) {
  if (largest < recB.get(i).getWidth() * recB.get(i).getHeight()) {
    largestIndex = recB.get(i).getIndex();
   }
}

//Output smallest and largest rectangle
System.out.printf("The Smallest: Rectangle %d\n", smallestIndex);
System.out.printf("The Largest: Rectangle %d\n", largestIndex);