2016-04-06 7 views
0

için beklenen çıktı alınamıyor Kullanıcının türünde yerleşik bir grup geçerli dikdörtgenden en küçük ve en büyük dikdörtgenin dizinini çıkarmam istenir.Arralist kullanırken, looop

Geçerli dikdörtgeni arraylist 'recB'ye depolamak için bir filtreleme yaptım. Ancak, geçerli dikdörtgenlerden en büyük ve en küçük dikdörtgeni keşfettiğimde, asla hem en küçük hem de en büyük doğru çıktılar.

Dikdörtgenler 1-4 arası kullanıcılar tarafından dizin girişi ile etiketlenir. Ben doğru indeksi koyacağız ve daha sonra çıktı onları 'smallestIndex = 0' ve 'largestIndex = 0' adlı iki 'int' ilan ediyorum. Ama her zaman "En küçük: Dikdörtgen 0, En büyük: xxx" veya başka bir şekilde yazdırılır.

Yani "en küçükIndex" veya "en büyükIndex" öğelerinden biri doğru dizini hizalayamayabilir, ancak bunu çözemez. Geçersiz olanları belirlenmesi ve aşağıda geçerli olanları depolamasını kodları ekleyin:

//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); 

} }


Tıpkı RecTester revize Dikdörtgen sınıfını ve aşağıda bir çıkış koymak hatırlatır.

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; 
    } 
} 

Revize 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); 
    } 

} 

Giriş ve çıkış

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 
> 
+0

çalışacaktı 'almak (0)' 'almak, (i) 'olmalıydı burada. Daha kolay okuma için boşluk kullanın. –

+0

@JoopEggen öyle, ancak OP neden genişliği 'getX() 'değerinden çıkardıktan sonra yükseklik ile çarpıyor? Ayrıca, – tmaxxcar

+0

@JoopEggen alanını hesaplamak için bir yöntem çağırmak daha kolay olmaz, benim hatam, ikincinin en büyük olduğu döngünün ikincisine ihtiyacım var .get (i) ' – tmaxxcar

cevap

0

Birincisi, kodunuzu tekrar okumak ve aynı kullandığınızdan emin kılacak her şey için stil. Örneğin, en küçük dikdörtgeni hesaplamak için gelişmiş bir for döngüsünü ve en büyük hesabınızı hesaplamak için standart bir döngü kullanıyorsunuz.

İkinci olarak, aşağıdaki kodu

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); 
+0

Merhaba arkadaşlar, anlık cevabınız için teşekkürler! –

+0

@JunlongWang bu isteğinizi çözdüyse lütfen çözüm olarak işaretleyin, değilse, yeni hata – tmaxxcar

+0

Merhaba ile bir yorum cevap Merhaba, hızlı cevap için teşekkür ederiz! Onları aynı stilde yaptım ve .get (0) değerini en büyük .get (i) olarak değiştirdim. Ancak sorun gitmedi. 'RecB' boyutunu incelemek ve 'recB' içindeki içeriği yazdırmak için diğer iki kod satırını yazdım, tam olarak yazdığımda ne yazdığımı ve kurala (geçersiz/geçerli) uyulduğunu gösterir. Fakat en büyük ve en küçük olanı bulmak söz konusu olduğunda, hala En Küçük veya En Büyük, ve bazen de geçersiz dikdörtgenlerin endeksi için 'Dikdörtgen 0' gösterir. Gerçekten neler olduğunu bilmiyorum Bu problemin başka ihtimali hakkında düşünür müsün? –

İlgili konular