我遇到了一些问题 - 一个是我要调试的,另一个是我需要编写的程序。不正确的输出或不会编译
第一个我要用户输入的星球名称,将其转换为大写,然后通过枚举运行它。我做错了什么? import java.util。*;
public class DebugNine4 {
enum Planet {
MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
};
public static void main(String[] args) {
Planet planet;
String userEntry;
int position;
int comparison;
Scanner input = new Scanner(System.in);
System.out.print("Enter a planet in our solar system >> ");
planet = input.nextLine().toUpperCase();
planet = Planet.valueOf(planet);
System.out.println("You entered " + planet);
position = planet.ordinal();
System.out.println(planet + " is " + (position + 1)
+ " planet(s) from the sun");
}
}
第二个是输出不像我想要的那样。它回来了:
Cut Shampoo
而不是与我需要它做的信息 - 可以按服务名称,成本和时间排序。此代码在哪里出错?
- 首先你需要创建一个服务对象有三个数据字段。
接下来,您将需要创建6个服务对象的数组,在书中用数据填充它们从表9.6,420页,如:
service[0] = new Service("Cut",8.00,15);
使用扫描仪的对象,收集响应从这样一个问题:
"Sort services by (S)ervice, (P)rice, or (T)ime"
返回的所有基于像输入在正确的顺序列出3个格式化列的信息:按时间排序
:
Trim $6.00 5 minutes Shampoo $4.00 10 minutes
代码:
public class SalonReport {
public static void main(String[] args) {
// services listing with time and cost
Service[] myService = new Service[6];
myService[0] = new Service("Cut", 8.00, 15);
myService[1] = new Service("Shampoo", 4.00, 10);
myService[2] = new Service("Manicure", 18.00, 30);
myService[3] = new Service("Style", 48.00, 55);
myService[4] = new Service("Permanent", 18.00, 35);
myService[5] = new Service("Trim", 6.00, 5);
SortDescription(myService, myService.length);
System.out.println(myService[0].getServiceType() + " "
+ myService[1].getServiceType());
}
public static void SortDescription(Service[] array, int len) {
int a;
int b;
Service temp;
for (a = 0; a < len; ++a)
for (b = 0; b < len - 1; ++b) {
if (array[b].getServiceType().compareTo(
array[b + 1].getServiceType()) > 0)
;
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
}
class Service {
// declaring parameters
String servDescript;
double price;
int avgMin;
public Service(String s, double p, int m) { // constructor
servDescript = s;
price = p;
avgMin = m;
}
// method returning requested item -
public String getServiceType() {
return servDescript;
}
public double getPrice() {
return price;
}
public int getMinutes() {
return avgMin;
}
}
请为我解决我所有的问题,请在这里。不用了,谢谢。 –
*我在哪里出错了这个代码?*当你没有可靠的尝试去调试它。 –
我做错了什么? :你只是不告诉我们什么是问题 – chburd