简体   繁体   中英

how to find the smallest square surface for a specific city in array java

public clas Qyteti {
    public float getSiperfaqja() {
        return siperfaqja;
    }
}


public clas Shteti {
    Qyteti [] qytetet;

    public void qytetiMeIVogel() {

    }
}

This is my code, i created an obj array named qytetet My homework is to find the smallest city in array calculating by square surface. qytetiMeIVogel should to calculate the smallest square surface.

qyteti = city

siperfaqja = square surface

There isn't really much to go on, but here is how I would do it:

    public class City {
    private  int squareSpace[];
    public void getData(){
      Scanner sc = new Scanner(System.in);
      squareSpace[] = new int[10];
      System.out.println("Please enter the square spaces of 10 cities\n");
      for(int i=0;i<10;i++) {
        sqaureSpace[i]=sc.nextInt();
      }
      //sc.close();
    }
    public int findMin(){
      int minValue=squareSpace[0];
      for(int i=1;i<10;i++) {
        if(squareSpace[i]<minValue)
          minValue=squareSpace[i];
    }
 }

I have assumed a static value of 10 here, but you can go ahead and assume any value. Now all you need to do is call the getData() and findMin() wherever required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM