【发布时间】:2021-08-22 13:56:36
【问题描述】:
public class Phone {
private String Band;
double price;
String Category;
void setPrice(double newPrice)
{
price = newPrice;
}
double getPrice(){
return price;
}
void setBand(String newBand)
{
Band = newBand;
}
String getBand(){
return Band;
}
void setCategory(String newCategory){
Category = newCategory;
}
String getCategory()
{
return Category;
}
public String Category(double price){
switch(price){
case 1:
if (price>=8000){
Category= "Expensive";
break;
}
case 2:
if(price>=5000 && size<7000){
Category = "Normal";
break;
}
default:
Category = "Cheap";
}
return Category;
}
}
public class TestPhone{
public static void main (String[]args){
Phone PhoneN = new Phone();
PhoneN.setPrice=6500;
System.out.println(PhoneA.getCategory());
}
}
但是,结果为空。 (当我运行 TestPhone 类时)
实际上,它应该是“正常”。
我在代码中错误地设置了什么?
我只是尝试使用 getter 和 setter 方法,并尝试在 Category 中也应用。
还是这是价格数据类型的问题?
这是运营商的问题吗?
有什么问题?
谁能帮我?
非常感谢。
【问题讨论】:
-
对不起,应该是 System.out.println(PhoneN.getCategory());
-
请edit问题并正确格式化代码。
-
使用
switch和内部if有什么意义?您只能使用if else语句并执行此操作 -
@user14917213 您的测试代码从未为 PhoneN 的类别设置值,因此它为空。
-
什么是:
(price>=5000 && size<7000)?不应该是:(price>=5000 && price<8000)?size来自哪里?
标签: java switch-statement