2013-10-06 31 views
0

我遇到了一些问题 - 一个是我要调试的,另一个是我需要编写的程序。不正确的输出或不会编译

第一个我要用户输入的星球名称,将其转换为大写,然后通过枚举运行它。我做错了什么? 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 

而不是与我需要它做的信息 - 可以按服务名称,成本和时间排序。此代码在哪里出错?

  1. 首先你需要创建一个服务对象有三个数据字段。
  2. 接下来,您将需要创建6个服务对象的数组,在书中用数据填充它们从表9.6,420页,如:

    service[0] = new Service("Cut",8.00,15); 
    
  3. 使用扫描仪的对象,收集响应从这样一个问题:"Sort services by (S)ervice, (P)rice, or (T)ime"

  4. 返回的所有基于像输入在正确的顺序列出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; 
} 
} 
+1

请为我解决我所有的问题,请在这里。不用了,谢谢。 –

+0

*我在哪里出错了这个代码?*当你没有可靠的尝试去调试它。 –

+0

我做错了什么? :你只是不告诉我们什么是问题 – chburd

回答

0

首先似乎是:

planet = Planet.valueOf(input.nextLine().toUpperCase()); 

删除此行,因为你不能把字符串到枚举类型变量:

planet = input.nextLine().toUpperCase(); 

编辑:

public class DebugNine4 { 
enum Planet { 
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE 
}; 

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter a planet in our solar system >> "); 

    Planet planet = Planet.valueOf(input.nextLine().trim().toUpperCase()); 
    System.out.println("You entered " + planet); 

    int position = planet.ordinal(); 
    System.out.println(planet + " is " + (position + 1) 
      + " planet(s) from the sun"); 
} 
} 

上面的代码工作,注意您的输入。

尝试组合,如:火星,火星,火星等

+0

当我这样做时,我得到这个错误:线程“main”java.lang中的异常。错误:未解决的编译问题: \t类型不匹配:不能从字符串转换成DebugNine4.Planet \t在类型DebugNine4.Planet方法的valueOf(字符串)不适用于参数(DebugNine4.Planet) \t在DebugNine4。主(DebugNine4.java:16) –

0

对于第二个任务: 它打印您阵列正是你的代码,第一和第二服务英寸 要打印所有项目,请循环浏览并打印所有需要的字段。 考虑如果排序不是任务本身,则使用Arrays.sort(array, comparator)

-1
import java.util.Scanner; 

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 pos; 
       int comparison; 
       Scanner input = new Scanner(System.in); 
       System.out.print("Enter a planet in our solar system >> "); 
       userEntry = input.nextLine().toUpperCase(); 
       planet = Planet.valueOf(userEntry); 
       System.out.println("You entered " + planet); 
       pos = planet.ordinal(); 
       System.out.println(planet + " is " + (pos + 1) + " planet(s) from the sun"); 
      } 
     }