Java - 类和方法问题

时间:2017-10-27 03:05:59

标签: java class methods

我已经在这个应用程序上工作了一段时间,我有点沮丧但还没准备好放弃!我在编译时遇到问题。

我遇到的错误如下:

TestApartment.java:45: error: method isMatched in class TestApartment cannot be applied to given types;
                if(isMatched(apt1))
                   ^
  required: Apartment,int,double,int
  found: Apartment
  reason: actual and formal argument lists differ in length

TestApartment.java:50: error: method isMatched in class TestApartment cannot be applied to given types;
                if(isMatched(apt2))
                   ^
  required: Apartment,int,double,int
  found: Apartment
  reason: actual and formal argument lists differ in length

TestApartment.java:55: error: method isMatched in class TestApartment cannot be applied to given types;
                if(isMatched(apt3))
                   ^
  required: Apartment,int,double,int
  found: Apartment
  reason: actual and formal argument lists differ in length

TestApartment.java:60: error: method isMatched in class TestApartment cannot be applied to given types;
                if(isMatched(apt4))
                   ^
  required: Apartment,int,double,int
  found: Apartment
  reason: actual and formal argument lists differ in length

TestApartment.java:65: error: method isMatched in class TestApartment cannot be applied to given types;
                if(isMatched(apt5))
                   ^
  required: Apartment,int,double,int
  found: Apartment
  reason: actual and formal argument lists differ in length

我需要做些什么才能让它发挥作用?

public class Apartment
{   
    int AptNumber;
    int numBedrooms;
    double numBathrooms;
    int AptRent;

public Apartment(int num, int beds, double baths, int rent)
{

}


public void setAptNumber(int num)// Accessor method for apartment number
{
    AptNumber = num;    
}

public int getAptNumber() // Gets the apartment number
{   
    return AptNumber;
}
public void setnumBedrooms(int beds) // Accessor method for bedrooms
{
    int numBedrooms;
    numBedrooms = beds;
}
public int getnumBedrooms()  // Gets the number of bedrooms
{
    return numBedrooms;
}
    public void setnumBathrooms(double baths) // Gets the number of bathrooms
{
    double numBathrooms;
    numBathrooms = baths;
    }
public double getnumBathrooms()  // Accessor method for bathrooms
{
    return numBathrooms;
}
public void setAptRent(int rent) // Accessor for rent
{
    int AptRent;
    AptRent = rent;
}
public int getAptRent() // Gets the amount of rent
{
    return AptRent;
}
public void display(int num, int beds, double baths, int rent)
{

}
    public double display()
{
    return display();
}

}




import java.util.Scanner;

public class TestApartment

{


public static void main(String[] args)
{
    int bedrooms;
    double bathrooms;
    int rent;

    // This prints out my name
    System.out.println("Beth Salvatore");

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

    // This creates five different 
    // Apartment objects
    Apartment apt1 = new Apartment(101, 2, 1, 725);
    Apartment apt2 = new Apartment(102, 2, 1.5, 775);
    Apartment apt3 = new Apartment(103, 3, 2, 870);
    Apartment apt4 = new Apartment(104, 3, 2.5, 960);
    Apartment apt5 = new Apartment(105, 3, 3, 1100);

    String isMatchedMsg = "Below are the apartments that match your search criteria: ";
    String notMatchedMsg = "No matches were found.";

    if(isMatched(apt1))
     display(apt1, isMatchedMsg);
    else
     display(apt1, notMatchedMsg);

    if(isMatched(apt2))
     display(apt2, isMatchedMsg);
    else
     display(apt2, notMatchedMsg);

    if(isMatched(apt3))
     display(apt3, isMatchedMsg);
    else
     display(apt3, notMatchedMsg);

    if(isMatched(apt4))
     display(apt4, isMatchedMsg);
    else
     display(apt4, notMatchedMsg);

    if(isMatched(apt5))
     display(apt5, isMatchedMsg);
    else
     display(apt5, notMatchedMsg);
}   

     public static boolean isMatched(Apartment apt, int bedrooms, double bathrooms, int rent)
   {

  int count = 0;
  int MIN_MATCH = 3;
  boolean isMatch;

if (apt.numBedrooms >= bedrooms)
    count = count + 1;  
    if (apt.numBathrooms >= bathrooms)
        count = count + 1;
    if (apt.AptRent <= rent)
        count = count + 1;
    if(count >= MIN_MATCH)
        isMatch = true;
else
    isMatch = false;

  return isMatch;
   }
   public static void display(Apartment apt, String msg)
   {
    System.out.println("Here are your results your results: " +
    "\nApartment number: " + apt.getAptNumber() + "." +
    "\nNumber of bedrooms: " + apt.getnumBedrooms() + "." +
    "\nNumber of bathrooms: " + apt.getnumBathrooms() + "." + 
    "\nRent: " + apt.getAptRent() + ".\n");
}           
}

1 个答案:

答案 0 :(得分:2)

您的方法public static boolean isMatched(Apartment apt, int bedrooms, double bathrooms, int rent)需要4个参数。你怎么能用1参数调用呢?

如下所示:

if(isMatched(apt, bedrooms, bathrooms, rent))