gpt4 book ai didi

java - 不兼容的类型问题

转载 作者:行者123 更新时间:2023-11-29 05:11:16 35 4
gpt4 key购买 nike

我正在尝试运行此程序,该程序必须包含一个 Building 类,该类由另外 2 个类 TradeCenterSportCenter 扩展。该程序必须打印 3 个建筑物(数组)的信息。问题是我不断收到数组中 int 的 Incompatible types issue 错误,我不确定如何修复它,因为我是新手。有人可以帮忙吗?

class Building {
String Name;
String Addres;
private int floors;

Building(String N, String A, int F) {
Name = N;
Addres = A;
floors = F;
}

public void SetFloors(int f) {
if (f <= 0)
floors = 1;
else
floors = f;
}

public int GetFloors() {
return floors;
}

void Print() {
System.out.print("Name=" + Name + "Addres=" + Addres + "floors=" + floors);
}
}

class TradingCenter extends Building {
String Owner;
String Brands[] = {"Brand"};

TradingCenter(String N, String A, int F, String O, String B[]) {
super(N, A, F);
Owner = O;
Brands = B;
}
}

class SportCenter extends Building {
String Sports[] = {"Sport"};
String Teams[] = {"Team"};

SportCenter(String N, String A, int F, String S[], String T[]) {
super(N, A, F);
Sports = S;
Teams = T;
}
}

class DemoBuilding {
public static void main(String args[]) {
Building arr[] = new Building[3];
arr[0] = new TradingCenter("Mall Plovdiv", "Smirnenski", 3, "Unknown", "Brand");
arr[1] = new SportCenter("ATLETIK", "Trakia", 1, "Sport1", "Team3");
arr[2] = new TradingCenter("Paradise Mall", "Sofia, bul. Cherni vruh 100", 5, "Unknown", "Brand");
for (int i = 0; i < arr.length; i++)
arr[i].Print();
}
}

最佳答案

因为您实际上传递的不是 int 作为参数,而是一个 String。此外,您只是传递字符串,而不是传递字符串 []。

您对 TradingCenter 构造函数的定义是

TradingCenter(String N, String A, int F, String O, String B[])  
// 3rd argument is supposed to be of type int here and the last(5th) argument is supposed to be of type String[]

您对 SportCenter 构造函数的定义是

SportCenter(String N, String A, int F, String S[], String T[])
// 3rd argument is supposed to be of type int here and the second-last(4th) and the last(5th) argument is supposed to be of type String[]

检查这些:-

arr[0]=new TradingCenter("Mall Plovdiv","Smirnenski","(int)3","Unknown","Brand 2");   
// "(int)3" is a String,not int; "Brand 2" is not a String Array
arr[1]=new SportCenter("ATLETIK","Trakia","(int)1","Sport 1","Team 3");
// "(int)1" is a String,not int; "Unknown" & "Brand 2" are Strings,not the String[]
arr[2]=new TradingCenter("Paradise Mall","Sofia, bul. Cherni vruh 100","(int)5","Unknown","Brand 2");
// "(int)5" is a String,not int; "Brand 2" is not a String Array

To simply use 3,1,5 as an integer, pass 3,1,5 directly without quotes in the constructor calling .

Also,to use "Unknown" and "Brand 2" as String[], pass them as new String[]{"Unknown} and new String[]{"Brand 2"} in the constructor.

关于java - 不兼容的类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28351831/

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