gpt4 book ai didi

java - 类无法解析为类型?

转载 作者:行者123 更新时间:2023-12-01 18:47:38 27 4
gpt4 key购买 nike

我是否需要以不同的方式定义 SpaService 类,或者以某种方式导入它?我是java新手,并且已经被困在这个问题上有一段时间了。这段代码几乎是我教科书上代码的精确副本,所以我真的很困惑为什么它不能编译。


public class SpaService
{
private String serviceDescription;
private double price;
public void setServiceDescription(String service)
{
serviceDescription = service;
}
public void setPrice(double pr)
{
price = pr;
}
public String getServiceDescription()
{
return serviceDescription;
}
public double getPrice()
{
return price;
}
}

在单独的文件中:

import java.util.Scanner;

public class CreateSpaServices
{
public static void main(String[] args)
{
String service;
double price;
SpaService firstService = new SpaService();
SpaService secondService = new SpaService();
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter service >> ");
service = keyboard.nextLine();
System.out.print("Enter price >> ");
price = keyboard.nextDouble();
firstService.setServiceDescription(service);
firstService.setPrice(price);
keyboard.nextLine();
System.out.print("Enter service >> ");
service = keyboard.nextLine();
System.out.print("Enter price >> ");
price = keyboard.nextDouble();
secondService.setServiceDescription(service);
secondService.setPrice(price);
System.out.println("First service details:");
System.out.println(firstService.getServiceDescription() +
" $" + firstService.getPrice());
System.out.println("Second service details:");
System.out.println(secondService.getServiceDescription() +
" $" + secondService.getPrice());
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
SpaService cannot be resolved to a type
SpaService cannot be resolved to a type
SpaService cannot be resolved to a type
SpaService cannot be resolved to a type

at CreateSpaServices.main(CreateSpaServices.java:9)

编辑::通过将 SpeService 类放入与 CreateSpaServices 类(默认包中)相同的 src 内的单独包中并从所述包中导入该类,我能够使其正常运行。我是否需要在 IDE 中配置路径(我正在使用 Eclipse)?如果是这样,我应该如何处理,以便将来可以从完全独立的项目访问类?

最佳答案

编译错误意味着您没有事先修复错误。检查 SpaService 类中 IDE 中是否有任何内容下方有红线。

关于java - 类无法解析为类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59797969/

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