Java逻辑问题返回所有对象

时间:2017-11-03 19:44:50

标签: java methods boolean logic increment

我是Java新手,我遇到问题,找出下面代码返回所有对象的原因,而不是符合搜索条件的两个对象。我猜测它是我的布尔方法中的东西,但似乎无法确定它是什么。或者它与我的计数增量有关?

import java.util.Scanner;

public class TestApartment
{

    public static void main(String[] args)
    {
        // Variables to hold input
        int bedrooms;
        double bathrooms;
        int rent;

        // This creates five different 
        // Apartment objects
        Apartment aptOne = new Apartment(101, 2, 1, 725);
        Apartment aptTwo = new Apartment(102, 2, 1.5, 775);
        Apartment aptThree = new Apartment(103, 3, 2, 870);
        Apartment aptFour = new Apartment(104, 3, 2.5, 960);
        Apartment aptFive = new Apartment(105, 3, 3, 1100);

        // Uses Scanner class to accept keyboard input
        Scanner keyboard = new Scanner(System.in);

        // Prompt user for input for minimum number of bedrooms required    
        System.out.print("Enter the minimum number of bedrooms: ");
        bedrooms = keyboard.nextInt();
        // Prompt user for input for minimum number of bathrooms required
        System.out.print("Enter the minimum number of bathrooms: ");
        bathrooms = keyboard.nextDouble();
        // Prompt user for input for maximum amount of rent
        System.out.print("Enter the maximum amount of rent: ");
        rent = keyboard.nextInt();

        System.out.println("The following apartments match your search: ");

        displayMatch(aptOne);
        displayMatch(aptTwo);
        displayMatch(aptThree);
        displayMatch(aptFour);
        displayMatch(aptFive);

    }

    public static boolean isMatched(Apartment apt, int bedrooms, double bathrooms, int rent)
    {
        int count = 0;
        boolean isMatch;
        final int MIN_MATCH  = 3;

        if (apt.getnumBedrooms() >= bedrooms);
        count = count + 1;
        if (apt.getnumBathrooms() >= bathrooms);
        count = count + 1;
        if (apt.getAptRent() <= rent);
        count = count + 1;
        if(count >= MIN_MATCH)
            isMatch = true;
        else
            isMatch = false;
        System.out.println ("No matches were found.");
        return isMatch;

    }

    public static void displayMatch(Apartment apt)
    {
        // Display the matching apartments

        System.out.println("Apartment Number: "  + apt.getApartment());
        System.out.println("Number of Bedrooms: " + apt.getnumBedrooms());
        System.out.println("Number of Bathrooms: " + apt.getnumBathrooms());
        System.out.println("Rent Amount: $" + apt.getAptRent());
        System.out.println();

    }
}

0 个答案:

没有答案