gpt4 book ai didi

Java - 类和方法的问题

转载 作者:行者123 更新时间:2023-12-02 02:31:50 26 4
gpt4 key购买 nike

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

我收到的错误如下:

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 rooms, double Bathroom, intrent) 需要 4 个参数。那么如何用 1 个参数调用它呢?

像下面这样调用它:

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

关于Java - 类和方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com