[英]Java - Issue with Classes and 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");
}
}
您的方法public static boolean isMatched(Apartment apt, int bedrooms, double bathrooms, int rent)
采用4個參數。 那么如何用1個參數調用它呢?
如下調用:
if(isMatched(apt, bedrooms, bathrooms, rent))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.